1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C driver for Maxim MAX8925
5 * Copyright (C) 2009 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
8 #include <linux/kernel.h>
9 #include <linux/init.h>
10 #include <linux/platform_device.h>
11 #include <linux/i2c.h>
12 #include <linux/mfd/max8925.h>
13 #include <linux/slab.h>
15 #define RTC_I2C_ADDR 0x68
16 #define ADC_I2C_ADDR 0x47
18 static inline int max8925_read_device(struct i2c_client *i2c,
19 int reg, int bytes, void *dest)
24 ret = i2c_smbus_read_i2c_block_data(i2c, reg, bytes, dest);
26 ret = i2c_smbus_read_byte_data(i2c, reg);
29 *(unsigned char *)dest = (unsigned char)ret;
34 static inline int max8925_write_device(struct i2c_client *i2c,
35 int reg, int bytes, void *src)
40 buf[0] = (unsigned char)reg;
41 memcpy(&buf[1], src, bytes);
43 ret = i2c_master_send(i2c, buf, bytes + 1);
49 int max8925_reg_read(struct i2c_client *i2c, int reg)
51 struct max8925_chip *chip = i2c_get_clientdata(i2c);
52 unsigned char data = 0;
55 mutex_lock(&chip->io_lock);
56 ret = max8925_read_device(i2c, reg, 1, &data);
57 mutex_unlock(&chip->io_lock);
64 EXPORT_SYMBOL(max8925_reg_read);
66 int max8925_reg_write(struct i2c_client *i2c, int reg,
69 struct max8925_chip *chip = i2c_get_clientdata(i2c);
72 mutex_lock(&chip->io_lock);
73 ret = max8925_write_device(i2c, reg, 1, &data);
74 mutex_unlock(&chip->io_lock);
78 EXPORT_SYMBOL(max8925_reg_write);
80 int max8925_bulk_read(struct i2c_client *i2c, int reg,
81 int count, unsigned char *buf)
83 struct max8925_chip *chip = i2c_get_clientdata(i2c);
86 mutex_lock(&chip->io_lock);
87 ret = max8925_read_device(i2c, reg, count, buf);
88 mutex_unlock(&chip->io_lock);
92 EXPORT_SYMBOL(max8925_bulk_read);
94 int max8925_bulk_write(struct i2c_client *i2c, int reg,
95 int count, unsigned char *buf)
97 struct max8925_chip *chip = i2c_get_clientdata(i2c);
100 mutex_lock(&chip->io_lock);
101 ret = max8925_write_device(i2c, reg, count, buf);
102 mutex_unlock(&chip->io_lock);
106 EXPORT_SYMBOL(max8925_bulk_write);
108 int max8925_set_bits(struct i2c_client *i2c, int reg,
109 unsigned char mask, unsigned char data)
111 struct max8925_chip *chip = i2c_get_clientdata(i2c);
115 mutex_lock(&chip->io_lock);
116 ret = max8925_read_device(i2c, reg, 1, &value);
121 ret = max8925_write_device(i2c, reg, 1, &value);
123 mutex_unlock(&chip->io_lock);
126 EXPORT_SYMBOL(max8925_set_bits);
129 static const struct i2c_device_id max8925_id_table[] = {
134 static int max8925_dt_init(struct device_node *np, struct device *dev,
135 struct max8925_platform_data *pdata)
139 ret = of_property_read_u32(np, "maxim,tsc-irq", &pdata->tsc_irq);
141 dev_err(dev, "Not found maxim,tsc-irq property\n");
147 static int max8925_probe(struct i2c_client *client,
148 const struct i2c_device_id *id)
150 struct max8925_platform_data *pdata = dev_get_platdata(&client->dev);
151 struct max8925_chip *chip;
152 struct device_node *node = client->dev.of_node;
154 if (node && !pdata) {
155 /* parse DT to get platform data */
156 pdata = devm_kzalloc(&client->dev,
157 sizeof(struct max8925_platform_data),
162 if (max8925_dt_init(node, &client->dev, pdata))
165 pr_info("%s: platform data is missing\n", __func__);
169 chip = devm_kzalloc(&client->dev,
170 sizeof(struct max8925_chip), GFP_KERNEL);
174 chip->dev = &client->dev;
175 i2c_set_clientdata(client, chip);
176 dev_set_drvdata(chip->dev, chip);
177 mutex_init(&chip->io_lock);
179 chip->rtc = i2c_new_dummy_device(chip->i2c->adapter, RTC_I2C_ADDR);
180 if (IS_ERR(chip->rtc)) {
181 dev_err(chip->dev, "Failed to allocate I2C device for RTC\n");
182 return PTR_ERR(chip->rtc);
184 i2c_set_clientdata(chip->rtc, chip);
186 chip->adc = i2c_new_dummy_device(chip->i2c->adapter, ADC_I2C_ADDR);
187 if (IS_ERR(chip->adc)) {
188 dev_err(chip->dev, "Failed to allocate I2C device for ADC\n");
189 i2c_unregister_device(chip->rtc);
190 return PTR_ERR(chip->adc);
192 i2c_set_clientdata(chip->adc, chip);
194 device_init_wakeup(&client->dev, 1);
196 max8925_device_init(chip, pdata);
201 static int max8925_remove(struct i2c_client *client)
203 struct max8925_chip *chip = i2c_get_clientdata(client);
205 max8925_device_exit(chip);
206 i2c_unregister_device(chip->adc);
207 i2c_unregister_device(chip->rtc);
211 #ifdef CONFIG_PM_SLEEP
212 static int max8925_suspend(struct device *dev)
214 struct i2c_client *client = to_i2c_client(dev);
215 struct max8925_chip *chip = i2c_get_clientdata(client);
217 if (device_may_wakeup(dev) && chip->wakeup_flag)
218 enable_irq_wake(chip->core_irq);
222 static int max8925_resume(struct device *dev)
224 struct i2c_client *client = to_i2c_client(dev);
225 struct max8925_chip *chip = i2c_get_clientdata(client);
227 if (device_may_wakeup(dev) && chip->wakeup_flag)
228 disable_irq_wake(chip->core_irq);
233 static SIMPLE_DEV_PM_OPS(max8925_pm_ops, max8925_suspend, max8925_resume);
235 static const struct of_device_id max8925_dt_ids[] = {
236 { .compatible = "maxim,max8925", },
240 static struct i2c_driver max8925_driver = {
243 .pm = &max8925_pm_ops,
244 .of_match_table = max8925_dt_ids,
246 .probe = max8925_probe,
247 .remove = max8925_remove,
248 .id_table = max8925_id_table,
251 static int __init max8925_i2c_init(void)
255 ret = i2c_add_driver(&max8925_driver);
257 pr_err("Failed to register MAX8925 I2C driver: %d\n", ret);
261 subsys_initcall(max8925_i2c_init);