Loading...
1/*
2 * Base driver for Analog Devices ADP5520/ADP5501 MFD PMICs
3 * LCD Backlight: drivers/video/backlight/adp5520_bl
4 * LEDs : drivers/led/leds-adp5520
5 * GPIO : drivers/gpio/adp5520-gpio (ADP5520 only)
6 * Keys : drivers/input/keyboard/adp5520-keys (ADP5520 only)
7 *
8 * Copyright 2009 Analog Devices Inc.
9 *
10 * Derived from da903x:
11 * Copyright (C) 2008 Compulab, Ltd.
12 * Mike Rapoport <mike@compulab.co.il>
13 *
14 * Copyright (C) 2006-2008 Marvell International Ltd.
15 * Eric Miao <eric.miao@marvell.com>
16 *
17 * Licensed under the GPL-2 or later.
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/platform_device.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/err.h>
28#include <linux/i2c.h>
29
30#include <linux/mfd/adp5520.h>
31
32struct adp5520_chip {
33 struct i2c_client *client;
34 struct device *dev;
35 struct mutex lock;
36 struct blocking_notifier_head notifier_list;
37 int irq;
38 unsigned long id;
39};
40
41static int __adp5520_read(struct i2c_client *client,
42 int reg, uint8_t *val)
43{
44 int ret;
45
46 ret = i2c_smbus_read_byte_data(client, reg);
47 if (ret < 0) {
48 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
49 return ret;
50 }
51
52 *val = (uint8_t)ret;
53 return 0;
54}
55
56static int __adp5520_write(struct i2c_client *client,
57 int reg, uint8_t val)
58{
59 int ret;
60
61 ret = i2c_smbus_write_byte_data(client, reg, val);
62 if (ret < 0) {
63 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
64 val, reg);
65 return ret;
66 }
67 return 0;
68}
69
70static int __adp5520_ack_bits(struct i2c_client *client, int reg,
71 uint8_t bit_mask)
72{
73 struct adp5520_chip *chip = i2c_get_clientdata(client);
74 uint8_t reg_val;
75 int ret;
76
77 mutex_lock(&chip->lock);
78
79 ret = __adp5520_read(client, reg, ®_val);
80
81 if (!ret) {
82 reg_val |= bit_mask;
83 ret = __adp5520_write(client, reg, reg_val);
84 }
85
86 mutex_unlock(&chip->lock);
87 return ret;
88}
89
90int adp5520_write(struct device *dev, int reg, uint8_t val)
91{
92 return __adp5520_write(to_i2c_client(dev), reg, val);
93}
94EXPORT_SYMBOL_GPL(adp5520_write);
95
96int adp5520_read(struct device *dev, int reg, uint8_t *val)
97{
98 return __adp5520_read(to_i2c_client(dev), reg, val);
99}
100EXPORT_SYMBOL_GPL(adp5520_read);
101
102int adp5520_set_bits(struct device *dev, int reg, uint8_t bit_mask)
103{
104 struct adp5520_chip *chip = dev_get_drvdata(dev);
105 uint8_t reg_val;
106 int ret;
107
108 mutex_lock(&chip->lock);
109
110 ret = __adp5520_read(chip->client, reg, ®_val);
111
112 if (!ret && ((reg_val & bit_mask) == 0)) {
113 reg_val |= bit_mask;
114 ret = __adp5520_write(chip->client, reg, reg_val);
115 }
116
117 mutex_unlock(&chip->lock);
118 return ret;
119}
120EXPORT_SYMBOL_GPL(adp5520_set_bits);
121
122int adp5520_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
123{
124 struct adp5520_chip *chip = dev_get_drvdata(dev);
125 uint8_t reg_val;
126 int ret;
127
128 mutex_lock(&chip->lock);
129
130 ret = __adp5520_read(chip->client, reg, ®_val);
131
132 if (!ret && (reg_val & bit_mask)) {
133 reg_val &= ~bit_mask;
134 ret = __adp5520_write(chip->client, reg, reg_val);
135 }
136
137 mutex_unlock(&chip->lock);
138 return ret;
139}
140EXPORT_SYMBOL_GPL(adp5520_clr_bits);
141
142int adp5520_register_notifier(struct device *dev, struct notifier_block *nb,
143 unsigned int events)
144{
145 struct adp5520_chip *chip = dev_get_drvdata(dev);
146
147 if (chip->irq) {
148 adp5520_set_bits(chip->dev, ADP5520_INTERRUPT_ENABLE,
149 events & (ADP5520_KP_IEN | ADP5520_KR_IEN |
150 ADP5520_OVP_IEN | ADP5520_CMPR_IEN));
151
152 return blocking_notifier_chain_register(&chip->notifier_list,
153 nb);
154 }
155
156 return -ENODEV;
157}
158EXPORT_SYMBOL_GPL(adp5520_register_notifier);
159
160int adp5520_unregister_notifier(struct device *dev, struct notifier_block *nb,
161 unsigned int events)
162{
163 struct adp5520_chip *chip = dev_get_drvdata(dev);
164
165 adp5520_clr_bits(chip->dev, ADP5520_INTERRUPT_ENABLE,
166 events & (ADP5520_KP_IEN | ADP5520_KR_IEN |
167 ADP5520_OVP_IEN | ADP5520_CMPR_IEN));
168
169 return blocking_notifier_chain_unregister(&chip->notifier_list, nb);
170}
171EXPORT_SYMBOL_GPL(adp5520_unregister_notifier);
172
173static irqreturn_t adp5520_irq_thread(int irq, void *data)
174{
175 struct adp5520_chip *chip = data;
176 unsigned int events;
177 uint8_t reg_val;
178 int ret;
179
180 ret = __adp5520_read(chip->client, ADP5520_MODE_STATUS, ®_val);
181 if (ret)
182 goto out;
183
184 events = reg_val & (ADP5520_OVP_INT | ADP5520_CMPR_INT |
185 ADP5520_GPI_INT | ADP5520_KR_INT | ADP5520_KP_INT);
186
187 blocking_notifier_call_chain(&chip->notifier_list, events, NULL);
188 /* ACK, Sticky bits are W1C */
189 __adp5520_ack_bits(chip->client, ADP5520_MODE_STATUS, events);
190
191out:
192 return IRQ_HANDLED;
193}
194
195static int __remove_subdev(struct device *dev, void *unused)
196{
197 platform_device_unregister(to_platform_device(dev));
198 return 0;
199}
200
201static int adp5520_remove_subdevs(struct adp5520_chip *chip)
202{
203 return device_for_each_child(chip->dev, NULL, __remove_subdev);
204}
205
206static int __devinit adp5520_probe(struct i2c_client *client,
207 const struct i2c_device_id *id)
208{
209 struct adp5520_platform_data *pdata = client->dev.platform_data;
210 struct platform_device *pdev;
211 struct adp5520_chip *chip;
212 int ret;
213
214 if (!i2c_check_functionality(client->adapter,
215 I2C_FUNC_SMBUS_BYTE_DATA)) {
216 dev_err(&client->dev, "SMBUS Word Data not Supported\n");
217 return -EIO;
218 }
219
220 if (pdata == NULL) {
221 dev_err(&client->dev, "missing platform data\n");
222 return -ENODEV;
223 }
224
225 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
226 if (!chip)
227 return -ENOMEM;
228
229 i2c_set_clientdata(client, chip);
230 chip->client = client;
231
232 chip->dev = &client->dev;
233 chip->irq = client->irq;
234 chip->id = id->driver_data;
235 mutex_init(&chip->lock);
236
237 if (chip->irq) {
238 BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
239
240 ret = request_threaded_irq(chip->irq, NULL, adp5520_irq_thread,
241 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
242 "adp5520", chip);
243 if (ret) {
244 dev_err(&client->dev, "failed to request irq %d\n",
245 chip->irq);
246 goto out_free_chip;
247 }
248 }
249
250 ret = adp5520_write(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY);
251 if (ret) {
252 dev_err(&client->dev, "failed to write\n");
253 goto out_free_irq;
254 }
255
256 if (pdata->keys) {
257 pdev = platform_device_register_data(chip->dev, "adp5520-keys",
258 chip->id, pdata->keys, sizeof(*pdata->keys));
259 if (IS_ERR(pdev)) {
260 ret = PTR_ERR(pdev);
261 goto out_remove_subdevs;
262 }
263 }
264
265 if (pdata->gpio) {
266 pdev = platform_device_register_data(chip->dev, "adp5520-gpio",
267 chip->id, pdata->gpio, sizeof(*pdata->gpio));
268 if (IS_ERR(pdev)) {
269 ret = PTR_ERR(pdev);
270 goto out_remove_subdevs;
271 }
272 }
273
274 if (pdata->leds) {
275 pdev = platform_device_register_data(chip->dev, "adp5520-led",
276 chip->id, pdata->leds, sizeof(*pdata->leds));
277 if (IS_ERR(pdev)) {
278 ret = PTR_ERR(pdev);
279 goto out_remove_subdevs;
280 }
281 }
282
283 if (pdata->backlight) {
284 pdev = platform_device_register_data(chip->dev,
285 "adp5520-backlight",
286 chip->id,
287 pdata->backlight,
288 sizeof(*pdata->backlight));
289 if (IS_ERR(pdev)) {
290 ret = PTR_ERR(pdev);
291 goto out_remove_subdevs;
292 }
293 }
294
295 return 0;
296
297out_remove_subdevs:
298 adp5520_remove_subdevs(chip);
299
300out_free_irq:
301 if (chip->irq)
302 free_irq(chip->irq, chip);
303
304out_free_chip:
305 kfree(chip);
306
307 return ret;
308}
309
310static int __devexit adp5520_remove(struct i2c_client *client)
311{
312 struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
313
314 if (chip->irq)
315 free_irq(chip->irq, chip);
316
317 adp5520_remove_subdevs(chip);
318 adp5520_write(chip->dev, ADP5520_MODE_STATUS, 0);
319 kfree(chip);
320 return 0;
321}
322
323#ifdef CONFIG_PM
324static int adp5520_suspend(struct device *dev)
325{
326 struct i2c_client *client = to_i2c_client(dev);
327 struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
328
329 adp5520_clr_bits(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY);
330 return 0;
331}
332
333static int adp5520_resume(struct device *dev)
334{
335 struct i2c_client *client = to_i2c_client(dev);
336 struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
337
338 adp5520_set_bits(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY);
339 return 0;
340}
341#endif
342
343static SIMPLE_DEV_PM_OPS(adp5520_pm, adp5520_suspend, adp5520_resume);
344
345static const struct i2c_device_id adp5520_id[] = {
346 { "pmic-adp5520", ID_ADP5520 },
347 { "pmic-adp5501", ID_ADP5501 },
348 { }
349};
350MODULE_DEVICE_TABLE(i2c, adp5520_id);
351
352static struct i2c_driver adp5520_driver = {
353 .driver = {
354 .name = "adp5520",
355 .owner = THIS_MODULE,
356 .pm = &adp5520_pm,
357 },
358 .probe = adp5520_probe,
359 .remove = __devexit_p(adp5520_remove),
360 .id_table = adp5520_id,
361};
362
363static int __init adp5520_init(void)
364{
365 return i2c_add_driver(&adp5520_driver);
366}
367module_init(adp5520_init);
368
369static void __exit adp5520_exit(void)
370{
371 i2c_del_driver(&adp5520_driver);
372}
373module_exit(adp5520_exit);
374
375MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
376MODULE_DESCRIPTION("ADP5520(01) PMIC-MFD Driver");
377MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Base driver for Analog Devices ADP5520/ADP5501 MFD PMICs
4 * LCD Backlight: drivers/video/backlight/adp5520_bl
5 * LEDs : drivers/led/leds-adp5520
6 * GPIO : drivers/gpio/adp5520-gpio (ADP5520 only)
7 * Keys : drivers/input/keyboard/adp5520-keys (ADP5520 only)
8 *
9 * Copyright 2009 Analog Devices Inc.
10 *
11 * Author: Michael Hennerich <michael.hennerich@analog.com>
12 *
13 * Derived from da903x:
14 * Copyright (C) 2008 Compulab, Ltd.
15 * Mike Rapoport <mike@compulab.co.il>
16 *
17 * Copyright (C) 2006-2008 Marvell International Ltd.
18 * Eric Miao <eric.miao@marvell.com>
19 */
20
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/platform_device.h>
24#include <linux/slab.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/err.h>
28#include <linux/i2c.h>
29
30#include <linux/mfd/adp5520.h>
31
32struct adp5520_chip {
33 struct i2c_client *client;
34 struct device *dev;
35 struct mutex lock;
36 struct blocking_notifier_head notifier_list;
37 int irq;
38 unsigned long id;
39 uint8_t mode;
40};
41
42static int __adp5520_read(struct i2c_client *client,
43 int reg, uint8_t *val)
44{
45 int ret;
46
47 ret = i2c_smbus_read_byte_data(client, reg);
48 if (ret < 0) {
49 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
50 return ret;
51 }
52
53 *val = (uint8_t)ret;
54 return 0;
55}
56
57static int __adp5520_write(struct i2c_client *client,
58 int reg, uint8_t val)
59{
60 int ret;
61
62 ret = i2c_smbus_write_byte_data(client, reg, val);
63 if (ret < 0) {
64 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
65 val, reg);
66 return ret;
67 }
68 return 0;
69}
70
71static int __adp5520_ack_bits(struct i2c_client *client, int reg,
72 uint8_t bit_mask)
73{
74 struct adp5520_chip *chip = i2c_get_clientdata(client);
75 uint8_t reg_val;
76 int ret;
77
78 mutex_lock(&chip->lock);
79
80 ret = __adp5520_read(client, reg, ®_val);
81
82 if (!ret) {
83 reg_val |= bit_mask;
84 ret = __adp5520_write(client, reg, reg_val);
85 }
86
87 mutex_unlock(&chip->lock);
88 return ret;
89}
90
91int adp5520_write(struct device *dev, int reg, uint8_t val)
92{
93 return __adp5520_write(to_i2c_client(dev), reg, val);
94}
95EXPORT_SYMBOL_GPL(adp5520_write);
96
97int adp5520_read(struct device *dev, int reg, uint8_t *val)
98{
99 return __adp5520_read(to_i2c_client(dev), reg, val);
100}
101EXPORT_SYMBOL_GPL(adp5520_read);
102
103int adp5520_set_bits(struct device *dev, int reg, uint8_t bit_mask)
104{
105 struct adp5520_chip *chip = dev_get_drvdata(dev);
106 uint8_t reg_val;
107 int ret;
108
109 mutex_lock(&chip->lock);
110
111 ret = __adp5520_read(chip->client, reg, ®_val);
112
113 if (!ret && ((reg_val & bit_mask) != bit_mask)) {
114 reg_val |= bit_mask;
115 ret = __adp5520_write(chip->client, reg, reg_val);
116 }
117
118 mutex_unlock(&chip->lock);
119 return ret;
120}
121EXPORT_SYMBOL_GPL(adp5520_set_bits);
122
123int adp5520_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
124{
125 struct adp5520_chip *chip = dev_get_drvdata(dev);
126 uint8_t reg_val;
127 int ret;
128
129 mutex_lock(&chip->lock);
130
131 ret = __adp5520_read(chip->client, reg, ®_val);
132
133 if (!ret && (reg_val & bit_mask)) {
134 reg_val &= ~bit_mask;
135 ret = __adp5520_write(chip->client, reg, reg_val);
136 }
137
138 mutex_unlock(&chip->lock);
139 return ret;
140}
141EXPORT_SYMBOL_GPL(adp5520_clr_bits);
142
143int adp5520_register_notifier(struct device *dev, struct notifier_block *nb,
144 unsigned int events)
145{
146 struct adp5520_chip *chip = dev_get_drvdata(dev);
147
148 if (chip->irq) {
149 adp5520_set_bits(chip->dev, ADP5520_INTERRUPT_ENABLE,
150 events & (ADP5520_KP_IEN | ADP5520_KR_IEN |
151 ADP5520_OVP_IEN | ADP5520_CMPR_IEN));
152
153 return blocking_notifier_chain_register(&chip->notifier_list,
154 nb);
155 }
156
157 return -ENODEV;
158}
159EXPORT_SYMBOL_GPL(adp5520_register_notifier);
160
161int adp5520_unregister_notifier(struct device *dev, struct notifier_block *nb,
162 unsigned int events)
163{
164 struct adp5520_chip *chip = dev_get_drvdata(dev);
165
166 adp5520_clr_bits(chip->dev, ADP5520_INTERRUPT_ENABLE,
167 events & (ADP5520_KP_IEN | ADP5520_KR_IEN |
168 ADP5520_OVP_IEN | ADP5520_CMPR_IEN));
169
170 return blocking_notifier_chain_unregister(&chip->notifier_list, nb);
171}
172EXPORT_SYMBOL_GPL(adp5520_unregister_notifier);
173
174static irqreturn_t adp5520_irq_thread(int irq, void *data)
175{
176 struct adp5520_chip *chip = data;
177 unsigned int events;
178 uint8_t reg_val;
179 int ret;
180
181 ret = __adp5520_read(chip->client, ADP5520_MODE_STATUS, ®_val);
182 if (ret)
183 goto out;
184
185 events = reg_val & (ADP5520_OVP_INT | ADP5520_CMPR_INT |
186 ADP5520_GPI_INT | ADP5520_KR_INT | ADP5520_KP_INT);
187
188 blocking_notifier_call_chain(&chip->notifier_list, events, NULL);
189 /* ACK, Sticky bits are W1C */
190 __adp5520_ack_bits(chip->client, ADP5520_MODE_STATUS, events);
191
192out:
193 return IRQ_HANDLED;
194}
195
196static int __remove_subdev(struct device *dev, void *unused)
197{
198 platform_device_unregister(to_platform_device(dev));
199 return 0;
200}
201
202static int adp5520_remove_subdevs(struct adp5520_chip *chip)
203{
204 return device_for_each_child(chip->dev, NULL, __remove_subdev);
205}
206
207static int adp5520_probe(struct i2c_client *client,
208 const struct i2c_device_id *id)
209{
210 struct adp5520_platform_data *pdata = dev_get_platdata(&client->dev);
211 struct platform_device *pdev;
212 struct adp5520_chip *chip;
213 int ret;
214
215 if (!i2c_check_functionality(client->adapter,
216 I2C_FUNC_SMBUS_BYTE_DATA)) {
217 dev_err(&client->dev, "SMBUS Word Data not Supported\n");
218 return -EIO;
219 }
220
221 if (pdata == NULL) {
222 dev_err(&client->dev, "missing platform data\n");
223 return -ENODEV;
224 }
225
226 chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
227 if (!chip)
228 return -ENOMEM;
229
230 i2c_set_clientdata(client, chip);
231 chip->client = client;
232
233 chip->dev = &client->dev;
234 chip->irq = client->irq;
235 chip->id = id->driver_data;
236 mutex_init(&chip->lock);
237
238 if (chip->irq) {
239 BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
240
241 ret = request_threaded_irq(chip->irq, NULL, adp5520_irq_thread,
242 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
243 "adp5520", chip);
244 if (ret) {
245 dev_err(&client->dev, "failed to request irq %d\n",
246 chip->irq);
247 return ret;
248 }
249 }
250
251 ret = adp5520_write(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY);
252 if (ret) {
253 dev_err(&client->dev, "failed to write\n");
254 goto out_free_irq;
255 }
256
257 if (pdata->keys) {
258 pdev = platform_device_register_data(chip->dev, "adp5520-keys",
259 chip->id, pdata->keys, sizeof(*pdata->keys));
260 if (IS_ERR(pdev)) {
261 ret = PTR_ERR(pdev);
262 goto out_remove_subdevs;
263 }
264 }
265
266 if (pdata->gpio) {
267 pdev = platform_device_register_data(chip->dev, "adp5520-gpio",
268 chip->id, pdata->gpio, sizeof(*pdata->gpio));
269 if (IS_ERR(pdev)) {
270 ret = PTR_ERR(pdev);
271 goto out_remove_subdevs;
272 }
273 }
274
275 if (pdata->leds) {
276 pdev = platform_device_register_data(chip->dev, "adp5520-led",
277 chip->id, pdata->leds, sizeof(*pdata->leds));
278 if (IS_ERR(pdev)) {
279 ret = PTR_ERR(pdev);
280 goto out_remove_subdevs;
281 }
282 }
283
284 if (pdata->backlight) {
285 pdev = platform_device_register_data(chip->dev,
286 "adp5520-backlight",
287 chip->id,
288 pdata->backlight,
289 sizeof(*pdata->backlight));
290 if (IS_ERR(pdev)) {
291 ret = PTR_ERR(pdev);
292 goto out_remove_subdevs;
293 }
294 }
295
296 return 0;
297
298out_remove_subdevs:
299 adp5520_remove_subdevs(chip);
300
301out_free_irq:
302 if (chip->irq)
303 free_irq(chip->irq, chip);
304
305 return ret;
306}
307
308#ifdef CONFIG_PM_SLEEP
309static int adp5520_suspend(struct device *dev)
310{
311 struct i2c_client *client = to_i2c_client(dev);
312 struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
313
314 adp5520_read(chip->dev, ADP5520_MODE_STATUS, &chip->mode);
315 /* All other bits are W1C */
316 chip->mode &= ADP5520_BL_EN | ADP5520_DIM_EN | ADP5520_nSTNBY;
317 adp5520_write(chip->dev, ADP5520_MODE_STATUS, 0);
318 return 0;
319}
320
321static int adp5520_resume(struct device *dev)
322{
323 struct i2c_client *client = to_i2c_client(dev);
324 struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
325
326 adp5520_write(chip->dev, ADP5520_MODE_STATUS, chip->mode);
327 return 0;
328}
329#endif
330
331static SIMPLE_DEV_PM_OPS(adp5520_pm, adp5520_suspend, adp5520_resume);
332
333static const struct i2c_device_id adp5520_id[] = {
334 { "pmic-adp5520", ID_ADP5520 },
335 { "pmic-adp5501", ID_ADP5501 },
336 { }
337};
338
339static struct i2c_driver adp5520_driver = {
340 .driver = {
341 .name = "adp5520",
342 .pm = &adp5520_pm,
343 .suppress_bind_attrs = true,
344 },
345 .probe = adp5520_probe,
346 .id_table = adp5520_id,
347};
348builtin_i2c_driver(adp5520_driver);