WIP FPC-III support
[linux/fpc-iii.git] / drivers / mfd / da903x.c
bloba818fbb559888657bc5fc4cd5b948683d5104cf8
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Base driver for Dialog Semiconductor DA9030/DA9034
5 * Copyright (C) 2008 Compulab, Ltd.
6 * Mike Rapoport <mike@compulab.co.il>
8 * Copyright (C) 2006-2008 Marvell International Ltd.
9 * Eric Miao <eric.miao@marvell.com>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
16 #include <linux/i2c.h>
17 #include <linux/mfd/da903x.h>
18 #include <linux/slab.h>
20 #define DA9030_CHIP_ID 0x00
21 #define DA9030_EVENT_A 0x01
22 #define DA9030_EVENT_B 0x02
23 #define DA9030_EVENT_C 0x03
24 #define DA9030_STATUS 0x04
25 #define DA9030_IRQ_MASK_A 0x05
26 #define DA9030_IRQ_MASK_B 0x06
27 #define DA9030_IRQ_MASK_C 0x07
28 #define DA9030_SYS_CTRL_A 0x08
29 #define DA9030_SYS_CTRL_B 0x09
30 #define DA9030_FAULT_LOG 0x0a
32 #define DA9034_CHIP_ID 0x00
33 #define DA9034_EVENT_A 0x01
34 #define DA9034_EVENT_B 0x02
35 #define DA9034_EVENT_C 0x03
36 #define DA9034_EVENT_D 0x04
37 #define DA9034_STATUS_A 0x05
38 #define DA9034_STATUS_B 0x06
39 #define DA9034_IRQ_MASK_A 0x07
40 #define DA9034_IRQ_MASK_B 0x08
41 #define DA9034_IRQ_MASK_C 0x09
42 #define DA9034_IRQ_MASK_D 0x0a
43 #define DA9034_SYS_CTRL_A 0x0b
44 #define DA9034_SYS_CTRL_B 0x0c
45 #define DA9034_FAULT_LOG 0x0d
47 struct da903x_chip;
49 struct da903x_chip_ops {
50 int (*init_chip)(struct da903x_chip *);
51 int (*unmask_events)(struct da903x_chip *, unsigned int events);
52 int (*mask_events)(struct da903x_chip *, unsigned int events);
53 int (*read_events)(struct da903x_chip *, unsigned int *events);
54 int (*read_status)(struct da903x_chip *, unsigned int *status);
57 struct da903x_chip {
58 struct i2c_client *client;
59 struct device *dev;
60 const struct da903x_chip_ops *ops;
62 int type;
63 uint32_t events_mask;
65 struct mutex lock;
66 struct work_struct irq_work;
68 struct blocking_notifier_head notifier_list;
71 static inline int __da903x_read(struct i2c_client *client,
72 int reg, uint8_t *val)
74 int ret;
76 ret = i2c_smbus_read_byte_data(client, reg);
77 if (ret < 0) {
78 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
79 return ret;
82 *val = (uint8_t)ret;
83 return 0;
86 static inline int __da903x_reads(struct i2c_client *client, int reg,
87 int len, uint8_t *val)
89 int ret;
91 ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
92 if (ret < 0) {
93 dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
94 return ret;
96 return 0;
99 static inline int __da903x_write(struct i2c_client *client,
100 int reg, uint8_t val)
102 int ret;
104 ret = i2c_smbus_write_byte_data(client, reg, val);
105 if (ret < 0) {
106 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
107 val, reg);
108 return ret;
110 return 0;
113 static inline int __da903x_writes(struct i2c_client *client, int reg,
114 int len, uint8_t *val)
116 int ret;
118 ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
119 if (ret < 0) {
120 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
121 return ret;
123 return 0;
126 int da903x_register_notifier(struct device *dev, struct notifier_block *nb,
127 unsigned int events)
129 struct da903x_chip *chip = dev_get_drvdata(dev);
131 chip->ops->unmask_events(chip, events);
132 return blocking_notifier_chain_register(&chip->notifier_list, nb);
134 EXPORT_SYMBOL_GPL(da903x_register_notifier);
136 int da903x_unregister_notifier(struct device *dev, struct notifier_block *nb,
137 unsigned int events)
139 struct da903x_chip *chip = dev_get_drvdata(dev);
141 chip->ops->mask_events(chip, events);
142 return blocking_notifier_chain_unregister(&chip->notifier_list, nb);
144 EXPORT_SYMBOL_GPL(da903x_unregister_notifier);
146 int da903x_write(struct device *dev, int reg, uint8_t val)
148 return __da903x_write(to_i2c_client(dev), reg, val);
150 EXPORT_SYMBOL_GPL(da903x_write);
152 int da903x_writes(struct device *dev, int reg, int len, uint8_t *val)
154 return __da903x_writes(to_i2c_client(dev), reg, len, val);
156 EXPORT_SYMBOL_GPL(da903x_writes);
158 int da903x_read(struct device *dev, int reg, uint8_t *val)
160 return __da903x_read(to_i2c_client(dev), reg, val);
162 EXPORT_SYMBOL_GPL(da903x_read);
164 int da903x_reads(struct device *dev, int reg, int len, uint8_t *val)
166 return __da903x_reads(to_i2c_client(dev), reg, len, val);
168 EXPORT_SYMBOL_GPL(da903x_reads);
170 int da903x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
172 struct da903x_chip *chip = dev_get_drvdata(dev);
173 uint8_t reg_val;
174 int ret = 0;
176 mutex_lock(&chip->lock);
178 ret = __da903x_read(chip->client, reg, &reg_val);
179 if (ret)
180 goto out;
182 if ((reg_val & bit_mask) != bit_mask) {
183 reg_val |= bit_mask;
184 ret = __da903x_write(chip->client, reg, reg_val);
186 out:
187 mutex_unlock(&chip->lock);
188 return ret;
190 EXPORT_SYMBOL_GPL(da903x_set_bits);
192 int da903x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
194 struct da903x_chip *chip = dev_get_drvdata(dev);
195 uint8_t reg_val;
196 int ret = 0;
198 mutex_lock(&chip->lock);
200 ret = __da903x_read(chip->client, reg, &reg_val);
201 if (ret)
202 goto out;
204 if (reg_val & bit_mask) {
205 reg_val &= ~bit_mask;
206 ret = __da903x_write(chip->client, reg, reg_val);
208 out:
209 mutex_unlock(&chip->lock);
210 return ret;
212 EXPORT_SYMBOL_GPL(da903x_clr_bits);
214 int da903x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
216 struct da903x_chip *chip = dev_get_drvdata(dev);
217 uint8_t reg_val;
218 int ret = 0;
220 mutex_lock(&chip->lock);
222 ret = __da903x_read(chip->client, reg, &reg_val);
223 if (ret)
224 goto out;
226 if ((reg_val & mask) != val) {
227 reg_val = (reg_val & ~mask) | val;
228 ret = __da903x_write(chip->client, reg, reg_val);
230 out:
231 mutex_unlock(&chip->lock);
232 return ret;
234 EXPORT_SYMBOL_GPL(da903x_update);
236 int da903x_query_status(struct device *dev, unsigned int sbits)
238 struct da903x_chip *chip = dev_get_drvdata(dev);
239 unsigned int status = 0;
241 chip->ops->read_status(chip, &status);
242 return ((status & sbits) == sbits);
244 EXPORT_SYMBOL(da903x_query_status);
246 static int da9030_init_chip(struct da903x_chip *chip)
248 uint8_t chip_id;
249 int err;
251 err = __da903x_read(chip->client, DA9030_CHIP_ID, &chip_id);
252 if (err)
253 return err;
255 err = __da903x_write(chip->client, DA9030_SYS_CTRL_A, 0xE8);
256 if (err)
257 return err;
259 dev_info(chip->dev, "DA9030 (CHIP ID: 0x%02x) detected\n", chip_id);
260 return 0;
263 static int da9030_unmask_events(struct da903x_chip *chip, unsigned int events)
265 uint8_t v[3];
267 chip->events_mask &= ~events;
269 v[0] = (chip->events_mask & 0xff);
270 v[1] = (chip->events_mask >> 8) & 0xff;
271 v[2] = (chip->events_mask >> 16) & 0xff;
273 return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
276 static int da9030_mask_events(struct da903x_chip *chip, unsigned int events)
278 uint8_t v[3];
280 chip->events_mask |= events;
282 v[0] = (chip->events_mask & 0xff);
283 v[1] = (chip->events_mask >> 8) & 0xff;
284 v[2] = (chip->events_mask >> 16) & 0xff;
286 return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
289 static int da9030_read_events(struct da903x_chip *chip, unsigned int *events)
291 uint8_t v[3] = {0, 0, 0};
292 int ret;
294 ret = __da903x_reads(chip->client, DA9030_EVENT_A, 3, v);
295 if (ret < 0)
296 return ret;
298 *events = (v[2] << 16) | (v[1] << 8) | v[0];
299 return 0;
302 static int da9030_read_status(struct da903x_chip *chip, unsigned int *status)
304 return __da903x_read(chip->client, DA9030_STATUS, (uint8_t *)status);
307 static int da9034_init_chip(struct da903x_chip *chip)
309 uint8_t chip_id;
310 int err;
312 err = __da903x_read(chip->client, DA9034_CHIP_ID, &chip_id);
313 if (err)
314 return err;
316 err = __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0xE8);
317 if (err)
318 return err;
320 /* avoid SRAM power off during sleep*/
321 __da903x_write(chip->client, 0x10, 0x07);
322 __da903x_write(chip->client, 0x11, 0xff);
323 __da903x_write(chip->client, 0x12, 0xff);
325 /* Enable the ONKEY power down functionality */
326 __da903x_write(chip->client, DA9034_SYS_CTRL_B, 0x20);
327 __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0x60);
329 /* workaround to make LEDs work */
330 __da903x_write(chip->client, 0x90, 0x01);
331 __da903x_write(chip->client, 0xB0, 0x08);
333 /* make ADTV1 and SDTV1 effective */
334 __da903x_write(chip->client, 0x20, 0x00);
336 dev_info(chip->dev, "DA9034 (CHIP ID: 0x%02x) detected\n", chip_id);
337 return 0;
340 static int da9034_unmask_events(struct da903x_chip *chip, unsigned int events)
342 uint8_t v[4];
344 chip->events_mask &= ~events;
346 v[0] = (chip->events_mask & 0xff);
347 v[1] = (chip->events_mask >> 8) & 0xff;
348 v[2] = (chip->events_mask >> 16) & 0xff;
349 v[3] = (chip->events_mask >> 24) & 0xff;
351 return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
354 static int da9034_mask_events(struct da903x_chip *chip, unsigned int events)
356 uint8_t v[4];
358 chip->events_mask |= events;
360 v[0] = (chip->events_mask & 0xff);
361 v[1] = (chip->events_mask >> 8) & 0xff;
362 v[2] = (chip->events_mask >> 16) & 0xff;
363 v[3] = (chip->events_mask >> 24) & 0xff;
365 return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
368 static int da9034_read_events(struct da903x_chip *chip, unsigned int *events)
370 uint8_t v[4] = {0, 0, 0, 0};
371 int ret;
373 ret = __da903x_reads(chip->client, DA9034_EVENT_A, 4, v);
374 if (ret < 0)
375 return ret;
377 *events = (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | v[0];
378 return 0;
381 static int da9034_read_status(struct da903x_chip *chip, unsigned int *status)
383 uint8_t v[2] = {0, 0};
384 int ret = 0;
386 ret = __da903x_reads(chip->client, DA9034_STATUS_A, 2, v);
387 if (ret)
388 return ret;
390 *status = (v[1] << 8) | v[0];
391 return 0;
394 static void da903x_irq_work(struct work_struct *work)
396 struct da903x_chip *chip =
397 container_of(work, struct da903x_chip, irq_work);
398 unsigned int events = 0;
400 while (1) {
401 if (chip->ops->read_events(chip, &events))
402 break;
404 events &= ~chip->events_mask;
405 if (events == 0)
406 break;
408 blocking_notifier_call_chain(
409 &chip->notifier_list, events, NULL);
411 enable_irq(chip->client->irq);
414 static irqreturn_t da903x_irq_handler(int irq, void *data)
416 struct da903x_chip *chip = data;
418 disable_irq_nosync(irq);
419 (void)schedule_work(&chip->irq_work);
421 return IRQ_HANDLED;
424 static const struct da903x_chip_ops da903x_ops[] = {
425 [0] = {
426 .init_chip = da9030_init_chip,
427 .unmask_events = da9030_unmask_events,
428 .mask_events = da9030_mask_events,
429 .read_events = da9030_read_events,
430 .read_status = da9030_read_status,
432 [1] = {
433 .init_chip = da9034_init_chip,
434 .unmask_events = da9034_unmask_events,
435 .mask_events = da9034_mask_events,
436 .read_events = da9034_read_events,
437 .read_status = da9034_read_status,
441 static const struct i2c_device_id da903x_id_table[] = {
442 { "da9030", 0 },
443 { "da9034", 1 },
444 { },
446 MODULE_DEVICE_TABLE(i2c, da903x_id_table);
448 static int __remove_subdev(struct device *dev, void *unused)
450 platform_device_unregister(to_platform_device(dev));
451 return 0;
454 static int da903x_remove_subdevs(struct da903x_chip *chip)
456 return device_for_each_child(chip->dev, NULL, __remove_subdev);
459 static int da903x_add_subdevs(struct da903x_chip *chip,
460 struct da903x_platform_data *pdata)
462 struct da903x_subdev_info *subdev;
463 struct platform_device *pdev;
464 int i, ret = 0;
466 for (i = 0; i < pdata->num_subdevs; i++) {
467 subdev = &pdata->subdevs[i];
469 pdev = platform_device_alloc(subdev->name, subdev->id);
470 if (!pdev) {
471 ret = -ENOMEM;
472 goto failed;
475 pdev->dev.parent = chip->dev;
476 pdev->dev.platform_data = subdev->platform_data;
478 ret = platform_device_add(pdev);
479 if (ret) {
480 platform_device_put(pdev);
481 goto failed;
484 return 0;
486 failed:
487 da903x_remove_subdevs(chip);
488 return ret;
491 static int da903x_probe(struct i2c_client *client,
492 const struct i2c_device_id *id)
494 struct da903x_platform_data *pdata = dev_get_platdata(&client->dev);
495 struct da903x_chip *chip;
496 unsigned int tmp;
497 int ret;
499 chip = devm_kzalloc(&client->dev, sizeof(struct da903x_chip),
500 GFP_KERNEL);
501 if (chip == NULL)
502 return -ENOMEM;
504 chip->client = client;
505 chip->dev = &client->dev;
506 chip->ops = &da903x_ops[id->driver_data];
508 mutex_init(&chip->lock);
509 INIT_WORK(&chip->irq_work, da903x_irq_work);
510 BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
512 i2c_set_clientdata(client, chip);
514 ret = chip->ops->init_chip(chip);
515 if (ret)
516 return ret;
518 /* mask and clear all IRQs */
519 chip->events_mask = 0xffffffff;
520 chip->ops->mask_events(chip, chip->events_mask);
521 chip->ops->read_events(chip, &tmp);
523 ret = devm_request_irq(&client->dev, client->irq, da903x_irq_handler,
524 IRQF_TRIGGER_FALLING,
525 "da903x", chip);
526 if (ret) {
527 dev_err(&client->dev, "failed to request irq %d\n",
528 client->irq);
529 return ret;
532 return da903x_add_subdevs(chip, pdata);
535 static int da903x_remove(struct i2c_client *client)
537 struct da903x_chip *chip = i2c_get_clientdata(client);
539 da903x_remove_subdevs(chip);
540 return 0;
543 static struct i2c_driver da903x_driver = {
544 .driver = {
545 .name = "da903x",
547 .probe = da903x_probe,
548 .remove = da903x_remove,
549 .id_table = da903x_id_table,
552 static int __init da903x_init(void)
554 return i2c_add_driver(&da903x_driver);
556 subsys_initcall(da903x_init);
558 static void __exit da903x_exit(void)
560 i2c_del_driver(&da903x_driver);
562 module_exit(da903x_exit);
564 MODULE_DESCRIPTION("PMIC Driver for Dialog Semiconductor DA9034");
565 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
566 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
567 MODULE_LICENSE("GPL v2");