2 * I2C driver for Maxim MAX8925
4 * Copyright (C) 2009 Marvell International Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/i2c.h>
15 #include <linux/mfd/max8925.h>
16 #include <linux/slab.h>
18 #define RTC_I2C_ADDR 0x68
19 #define ADC_I2C_ADDR 0x47
21 static inline int max8925_read_device(struct i2c_client *i2c,
22 int reg, int bytes, void *dest)
27 ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest);
29 ret = i2c_smbus_read_byte_data(i2c, reg);
32 *(unsigned char *)dest = (unsigned char)ret;
37 static inline int max8925_write_device(struct i2c_client *i2c,
38 int reg, int bytes, void *src)
43 buf[0] = (unsigned char)reg;
44 memcpy(&buf[1], src, bytes);
46 ret = i2c_master_send(i2c, buf, bytes + 1);
52 int max8925_reg_read(struct i2c_client *i2c, int reg)
54 struct max8925_chip *chip = i2c_get_clientdata(i2c);
55 unsigned char data = 0;
58 mutex_lock(&chip->io_lock);
59 ret = max8925_read_device(i2c, reg, 1, &data);
60 mutex_unlock(&chip->io_lock);
67 EXPORT_SYMBOL(max8925_reg_read);
69 int max8925_reg_write(struct i2c_client *i2c, int reg,
72 struct max8925_chip *chip = i2c_get_clientdata(i2c);
75 mutex_lock(&chip->io_lock);
76 ret = max8925_write_device(i2c, reg, 1, &data);
77 mutex_unlock(&chip->io_lock);
81 EXPORT_SYMBOL(max8925_reg_write);
83 int max8925_bulk_read(struct i2c_client *i2c, int reg,
84 int count, unsigned char *buf)
86 struct max8925_chip *chip = i2c_get_clientdata(i2c);
89 mutex_lock(&chip->io_lock);
90 ret = max8925_read_device(i2c, reg, count, buf);
91 mutex_unlock(&chip->io_lock);
95 EXPORT_SYMBOL(max8925_bulk_read);
97 int max8925_bulk_write(struct i2c_client *i2c, int reg,
98 int count, unsigned char *buf)
100 struct max8925_chip *chip = i2c_get_clientdata(i2c);
103 mutex_lock(&chip->io_lock);
104 ret = max8925_write_device(i2c, reg, count, buf);
105 mutex_unlock(&chip->io_lock);
109 EXPORT_SYMBOL(max8925_bulk_write);
111 int max8925_set_bits(struct i2c_client *i2c, int reg,
112 unsigned char mask, unsigned char data)
114 struct max8925_chip *chip = i2c_get_clientdata(i2c);
118 mutex_lock(&chip->io_lock);
119 ret = max8925_read_device(i2c, reg, 1, &value);
124 ret = max8925_write_device(i2c, reg, 1, &value);
126 mutex_unlock(&chip->io_lock);
129 EXPORT_SYMBOL(max8925_set_bits);
132 static const struct i2c_device_id max8925_id_table[] = {
137 static int max8925_dt_init(struct device_node *np, struct device *dev,
138 struct max8925_platform_data *pdata)
142 ret = of_property_read_u32(np, "maxim,tsc-irq", &pdata->tsc_irq);
144 dev_err(dev, "Not found maxim,tsc-irq property\n");
150 static int max8925_probe(struct i2c_client *client,
151 const struct i2c_device_id *id)
153 struct max8925_platform_data *pdata = dev_get_platdata(&client->dev);
154 struct max8925_chip *chip;
155 struct device_node *node = client->dev.of_node;
157 if (node && !pdata) {
158 /* parse DT to get platform data */
159 pdata = devm_kzalloc(&client->dev,
160 sizeof(struct max8925_platform_data),
165 if (max8925_dt_init(node, &client->dev, pdata))
168 pr_info("%s: platform data is missing\n", __func__);
172 chip = devm_kzalloc(&client->dev,
173 sizeof(struct max8925_chip), GFP_KERNEL);
177 chip->dev = &client->dev;
178 i2c_set_clientdata(client, chip);
179 dev_set_drvdata(chip->dev, chip);
180 mutex_init(&chip->io_lock);
182 chip->rtc = i2c_new_dummy(chip->i2c->adapter, RTC_I2C_ADDR);
184 dev_err(chip->dev, "Failed to allocate I2C device for RTC\n");
187 i2c_set_clientdata(chip->rtc, chip);
189 chip->adc = i2c_new_dummy(chip->i2c->adapter, ADC_I2C_ADDR);
191 dev_err(chip->dev, "Failed to allocate I2C device for ADC\n");
192 i2c_unregister_device(chip->rtc);
195 i2c_set_clientdata(chip->adc, chip);
197 device_init_wakeup(&client->dev, 1);
199 max8925_device_init(chip, pdata);
204 static int max8925_remove(struct i2c_client *client)
206 struct max8925_chip *chip = i2c_get_clientdata(client);
208 max8925_device_exit(chip);
209 i2c_unregister_device(chip->adc);
210 i2c_unregister_device(chip->rtc);
214 #ifdef CONFIG_PM_SLEEP
215 static int max8925_suspend(struct device *dev)
217 struct i2c_client *client = to_i2c_client(dev);
218 struct max8925_chip *chip = i2c_get_clientdata(client);
220 if (device_may_wakeup(dev) && chip->wakeup_flag)
221 enable_irq_wake(chip->core_irq);
225 static int max8925_resume(struct device *dev)
227 struct i2c_client *client = to_i2c_client(dev);
228 struct max8925_chip *chip = i2c_get_clientdata(client);
230 if (device_may_wakeup(dev) && chip->wakeup_flag)
231 disable_irq_wake(chip->core_irq);
236 static SIMPLE_DEV_PM_OPS(max8925_pm_ops, max8925_suspend, max8925_resume);
238 static const struct of_device_id max8925_dt_ids[] = {
239 { .compatible = "maxim,max8925", },
243 static struct i2c_driver max8925_driver = {
246 .pm = &max8925_pm_ops,
247 .of_match_table = max8925_dt_ids,
249 .probe = max8925_probe,
250 .remove = max8925_remove,
251 .id_table = max8925_id_table,
254 static int __init max8925_i2c_init(void)
258 ret = i2c_add_driver(&max8925_driver);
260 pr_err("Failed to register MAX8925 I2C driver: %d\n", ret);
264 subsys_initcall(max8925_i2c_init);