1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * I2C driver for Maxim MAX8925 |
4 | * |
5 | * Copyright (C) 2009 Marvell International Ltd. |
6 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
7 | */ |
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> |
14 | |
15 | #define RTC_I2C_ADDR 0x68 |
16 | #define ADC_I2C_ADDR 0x47 |
17 | |
18 | static inline int max8925_read_device(struct i2c_client *i2c, |
19 | int reg, int bytes, void *dest) |
20 | { |
21 | int ret; |
22 | |
23 | if (bytes > 1) |
24 | ret = i2c_smbus_read_i2c_block_data(client: i2c, command: reg, length: bytes, values: dest); |
25 | else { |
26 | ret = i2c_smbus_read_byte_data(client: i2c, command: reg); |
27 | if (ret < 0) |
28 | return ret; |
29 | *(unsigned char *)dest = (unsigned char)ret; |
30 | } |
31 | return ret; |
32 | } |
33 | |
34 | static inline int max8925_write_device(struct i2c_client *i2c, |
35 | int reg, int bytes, void *src) |
36 | { |
37 | unsigned char buf[9]; |
38 | int ret; |
39 | |
40 | buf[0] = (unsigned char)reg; |
41 | memcpy(&buf[1], src, bytes); |
42 | |
43 | ret = i2c_master_send(client: i2c, buf, count: bytes + 1); |
44 | if (ret < 0) |
45 | return ret; |
46 | return 0; |
47 | } |
48 | |
49 | int max8925_reg_read(struct i2c_client *i2c, int reg) |
50 | { |
51 | struct max8925_chip *chip = i2c_get_clientdata(client: i2c); |
52 | unsigned char data = 0; |
53 | int ret; |
54 | |
55 | mutex_lock(&chip->io_lock); |
56 | ret = max8925_read_device(i2c, reg, bytes: 1, dest: &data); |
57 | mutex_unlock(lock: &chip->io_lock); |
58 | |
59 | if (ret < 0) |
60 | return ret; |
61 | else |
62 | return (int)data; |
63 | } |
64 | EXPORT_SYMBOL(max8925_reg_read); |
65 | |
66 | int max8925_reg_write(struct i2c_client *i2c, int reg, |
67 | unsigned char data) |
68 | { |
69 | struct max8925_chip *chip = i2c_get_clientdata(client: i2c); |
70 | int ret; |
71 | |
72 | mutex_lock(&chip->io_lock); |
73 | ret = max8925_write_device(i2c, reg, bytes: 1, src: &data); |
74 | mutex_unlock(lock: &chip->io_lock); |
75 | |
76 | return ret; |
77 | } |
78 | EXPORT_SYMBOL(max8925_reg_write); |
79 | |
80 | int max8925_bulk_read(struct i2c_client *i2c, int reg, |
81 | int count, unsigned char *buf) |
82 | { |
83 | struct max8925_chip *chip = i2c_get_clientdata(client: i2c); |
84 | int ret; |
85 | |
86 | mutex_lock(&chip->io_lock); |
87 | ret = max8925_read_device(i2c, reg, bytes: count, dest: buf); |
88 | mutex_unlock(lock: &chip->io_lock); |
89 | |
90 | return ret; |
91 | } |
92 | EXPORT_SYMBOL(max8925_bulk_read); |
93 | |
94 | int max8925_bulk_write(struct i2c_client *i2c, int reg, |
95 | int count, unsigned char *buf) |
96 | { |
97 | struct max8925_chip *chip = i2c_get_clientdata(client: i2c); |
98 | int ret; |
99 | |
100 | mutex_lock(&chip->io_lock); |
101 | ret = max8925_write_device(i2c, reg, bytes: count, src: buf); |
102 | mutex_unlock(lock: &chip->io_lock); |
103 | |
104 | return ret; |
105 | } |
106 | EXPORT_SYMBOL(max8925_bulk_write); |
107 | |
108 | int max8925_set_bits(struct i2c_client *i2c, int reg, |
109 | unsigned char mask, unsigned char data) |
110 | { |
111 | struct max8925_chip *chip = i2c_get_clientdata(client: i2c); |
112 | unsigned char value; |
113 | int ret; |
114 | |
115 | mutex_lock(&chip->io_lock); |
116 | ret = max8925_read_device(i2c, reg, bytes: 1, dest: &value); |
117 | if (ret < 0) |
118 | goto out; |
119 | value &= ~mask; |
120 | value |= data; |
121 | ret = max8925_write_device(i2c, reg, bytes: 1, src: &value); |
122 | out: |
123 | mutex_unlock(lock: &chip->io_lock); |
124 | return ret; |
125 | } |
126 | EXPORT_SYMBOL(max8925_set_bits); |
127 | |
128 | |
129 | static const struct i2c_device_id max8925_id_table[] = { |
130 | { "max8925" , 0 }, |
131 | { }, |
132 | }; |
133 | |
134 | static int max8925_dt_init(struct device_node *np, struct device *dev, |
135 | struct max8925_platform_data *pdata) |
136 | { |
137 | int ret; |
138 | |
139 | ret = of_property_read_u32(np, propname: "maxim,tsc-irq" , out_value: &pdata->tsc_irq); |
140 | if (ret) { |
141 | dev_err(dev, "Not found maxim,tsc-irq property\n" ); |
142 | return -EINVAL; |
143 | } |
144 | return 0; |
145 | } |
146 | |
147 | static int max8925_probe(struct i2c_client *client) |
148 | { |
149 | struct max8925_platform_data *pdata = dev_get_platdata(dev: &client->dev); |
150 | struct max8925_chip *chip; |
151 | struct device_node *node = client->dev.of_node; |
152 | |
153 | if (node && !pdata) { |
154 | /* parse DT to get platform data */ |
155 | pdata = devm_kzalloc(dev: &client->dev, |
156 | size: sizeof(struct max8925_platform_data), |
157 | GFP_KERNEL); |
158 | if (!pdata) |
159 | return -ENOMEM; |
160 | |
161 | if (max8925_dt_init(np: node, dev: &client->dev, pdata)) |
162 | return -EINVAL; |
163 | } else if (!pdata) { |
164 | pr_info("%s: platform data is missing\n" , __func__); |
165 | return -EINVAL; |
166 | } |
167 | |
168 | chip = devm_kzalloc(dev: &client->dev, |
169 | size: sizeof(struct max8925_chip), GFP_KERNEL); |
170 | if (chip == NULL) |
171 | return -ENOMEM; |
172 | chip->i2c = client; |
173 | chip->dev = &client->dev; |
174 | i2c_set_clientdata(client, data: chip); |
175 | mutex_init(&chip->io_lock); |
176 | |
177 | chip->rtc = i2c_new_dummy_device(adapter: chip->i2c->adapter, RTC_I2C_ADDR); |
178 | if (IS_ERR(ptr: chip->rtc)) { |
179 | dev_err(chip->dev, "Failed to allocate I2C device for RTC\n" ); |
180 | return PTR_ERR(ptr: chip->rtc); |
181 | } |
182 | i2c_set_clientdata(client: chip->rtc, data: chip); |
183 | |
184 | chip->adc = i2c_new_dummy_device(adapter: chip->i2c->adapter, ADC_I2C_ADDR); |
185 | if (IS_ERR(ptr: chip->adc)) { |
186 | dev_err(chip->dev, "Failed to allocate I2C device for ADC\n" ); |
187 | i2c_unregister_device(client: chip->rtc); |
188 | return PTR_ERR(ptr: chip->adc); |
189 | } |
190 | i2c_set_clientdata(client: chip->adc, data: chip); |
191 | |
192 | device_init_wakeup(dev: &client->dev, enable: 1); |
193 | |
194 | max8925_device_init(chip, pdata); |
195 | |
196 | return 0; |
197 | } |
198 | |
199 | static void max8925_remove(struct i2c_client *client) |
200 | { |
201 | struct max8925_chip *chip = i2c_get_clientdata(client); |
202 | |
203 | max8925_device_exit(chip); |
204 | i2c_unregister_device(client: chip->adc); |
205 | i2c_unregister_device(client: chip->rtc); |
206 | } |
207 | |
208 | static int max8925_suspend(struct device *dev) |
209 | { |
210 | struct i2c_client *client = to_i2c_client(dev); |
211 | struct max8925_chip *chip = i2c_get_clientdata(client); |
212 | |
213 | if (device_may_wakeup(dev) && chip->wakeup_flag) |
214 | enable_irq_wake(irq: chip->core_irq); |
215 | return 0; |
216 | } |
217 | |
218 | static int max8925_resume(struct device *dev) |
219 | { |
220 | struct i2c_client *client = to_i2c_client(dev); |
221 | struct max8925_chip *chip = i2c_get_clientdata(client); |
222 | |
223 | if (device_may_wakeup(dev) && chip->wakeup_flag) |
224 | disable_irq_wake(irq: chip->core_irq); |
225 | return 0; |
226 | } |
227 | |
228 | static DEFINE_SIMPLE_DEV_PM_OPS(max8925_pm_ops, |
229 | max8925_suspend, max8925_resume); |
230 | |
231 | static const struct of_device_id max8925_dt_ids[] = { |
232 | { .compatible = "maxim,max8925" , }, |
233 | {}, |
234 | }; |
235 | |
236 | static struct i2c_driver max8925_driver = { |
237 | .driver = { |
238 | .name = "max8925" , |
239 | .pm = pm_sleep_ptr(&max8925_pm_ops), |
240 | .of_match_table = max8925_dt_ids, |
241 | }, |
242 | .probe = max8925_probe, |
243 | .remove = max8925_remove, |
244 | .id_table = max8925_id_table, |
245 | }; |
246 | |
247 | static int __init max8925_i2c_init(void) |
248 | { |
249 | int ret; |
250 | |
251 | ret = i2c_add_driver(&max8925_driver); |
252 | if (ret != 0) |
253 | pr_err("Failed to register MAX8925 I2C driver: %d\n" , ret); |
254 | |
255 | return ret; |
256 | } |
257 | subsys_initcall(max8925_i2c_init); |
258 | |