Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Base driver for Dialog Semiconductor DA9030/DA9034
0004  *
0005  * Copyright (C) 2008 Compulab, Ltd.
0006  *  Mike Rapoport <mike@compulab.co.il>
0007  *
0008  * Copyright (C) 2006-2008 Marvell International Ltd.
0009  *  Eric Miao <eric.miao@marvell.com>
0010  */
0011 
0012 #include <linux/kernel.h>
0013 #include <linux/module.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/platform_device.h>
0016 #include <linux/i2c.h>
0017 #include <linux/mfd/da903x.h>
0018 #include <linux/slab.h>
0019 
0020 #define DA9030_CHIP_ID      0x00
0021 #define DA9030_EVENT_A      0x01
0022 #define DA9030_EVENT_B      0x02
0023 #define DA9030_EVENT_C      0x03
0024 #define DA9030_STATUS       0x04
0025 #define DA9030_IRQ_MASK_A   0x05
0026 #define DA9030_IRQ_MASK_B   0x06
0027 #define DA9030_IRQ_MASK_C   0x07
0028 #define DA9030_SYS_CTRL_A   0x08
0029 #define DA9030_SYS_CTRL_B   0x09
0030 #define DA9030_FAULT_LOG    0x0a
0031 
0032 #define DA9034_CHIP_ID      0x00
0033 #define DA9034_EVENT_A      0x01
0034 #define DA9034_EVENT_B      0x02
0035 #define DA9034_EVENT_C      0x03
0036 #define DA9034_EVENT_D      0x04
0037 #define DA9034_STATUS_A     0x05
0038 #define DA9034_STATUS_B     0x06
0039 #define DA9034_IRQ_MASK_A   0x07
0040 #define DA9034_IRQ_MASK_B   0x08
0041 #define DA9034_IRQ_MASK_C   0x09
0042 #define DA9034_IRQ_MASK_D   0x0a
0043 #define DA9034_SYS_CTRL_A   0x0b
0044 #define DA9034_SYS_CTRL_B   0x0c
0045 #define DA9034_FAULT_LOG    0x0d
0046 
0047 struct da903x_chip;
0048 
0049 struct da903x_chip_ops {
0050     int (*init_chip)(struct da903x_chip *);
0051     int (*unmask_events)(struct da903x_chip *, unsigned int events);
0052     int (*mask_events)(struct da903x_chip *, unsigned int events);
0053     int (*read_events)(struct da903x_chip *, unsigned int *events);
0054     int (*read_status)(struct da903x_chip *, unsigned int *status);
0055 };
0056 
0057 struct da903x_chip {
0058     struct i2c_client   *client;
0059     struct device       *dev;
0060     const struct da903x_chip_ops *ops;
0061 
0062     int         type;
0063     uint32_t        events_mask;
0064 
0065     struct mutex        lock;
0066     struct work_struct  irq_work;
0067 
0068     struct blocking_notifier_head notifier_list;
0069 };
0070 
0071 static inline int __da903x_read(struct i2c_client *client,
0072                 int reg, uint8_t *val)
0073 {
0074     int ret;
0075 
0076     ret = i2c_smbus_read_byte_data(client, reg);
0077     if (ret < 0) {
0078         dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
0079         return ret;
0080     }
0081 
0082     *val = (uint8_t)ret;
0083     return 0;
0084 }
0085 
0086 static inline int __da903x_reads(struct i2c_client *client, int reg,
0087                  int len, uint8_t *val)
0088 {
0089     int ret;
0090 
0091     ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
0092     if (ret < 0) {
0093         dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
0094         return ret;
0095     }
0096     return 0;
0097 }
0098 
0099 static inline int __da903x_write(struct i2c_client *client,
0100                  int reg, uint8_t val)
0101 {
0102     int ret;
0103 
0104     ret = i2c_smbus_write_byte_data(client, reg, val);
0105     if (ret < 0) {
0106         dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
0107                 val, reg);
0108         return ret;
0109     }
0110     return 0;
0111 }
0112 
0113 static inline int __da903x_writes(struct i2c_client *client, int reg,
0114                   int len, uint8_t *val)
0115 {
0116     int ret;
0117 
0118     ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
0119     if (ret < 0) {
0120         dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
0121         return ret;
0122     }
0123     return 0;
0124 }
0125 
0126 int da903x_register_notifier(struct device *dev, struct notifier_block *nb,
0127                 unsigned int events)
0128 {
0129     struct da903x_chip *chip = dev_get_drvdata(dev);
0130 
0131     chip->ops->unmask_events(chip, events);
0132     return blocking_notifier_chain_register(&chip->notifier_list, nb);
0133 }
0134 EXPORT_SYMBOL_GPL(da903x_register_notifier);
0135 
0136 int da903x_unregister_notifier(struct device *dev, struct notifier_block *nb,
0137                 unsigned int events)
0138 {
0139     struct da903x_chip *chip = dev_get_drvdata(dev);
0140 
0141     chip->ops->mask_events(chip, events);
0142     return blocking_notifier_chain_unregister(&chip->notifier_list, nb);
0143 }
0144 EXPORT_SYMBOL_GPL(da903x_unregister_notifier);
0145 
0146 int da903x_write(struct device *dev, int reg, uint8_t val)
0147 {
0148     return __da903x_write(to_i2c_client(dev), reg, val);
0149 }
0150 EXPORT_SYMBOL_GPL(da903x_write);
0151 
0152 int da903x_writes(struct device *dev, int reg, int len, uint8_t *val)
0153 {
0154     return __da903x_writes(to_i2c_client(dev), reg, len, val);
0155 }
0156 EXPORT_SYMBOL_GPL(da903x_writes);
0157 
0158 int da903x_read(struct device *dev, int reg, uint8_t *val)
0159 {
0160     return __da903x_read(to_i2c_client(dev), reg, val);
0161 }
0162 EXPORT_SYMBOL_GPL(da903x_read);
0163 
0164 int da903x_reads(struct device *dev, int reg, int len, uint8_t *val)
0165 {
0166     return __da903x_reads(to_i2c_client(dev), reg, len, val);
0167 }
0168 EXPORT_SYMBOL_GPL(da903x_reads);
0169 
0170 int da903x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
0171 {
0172     struct da903x_chip *chip = dev_get_drvdata(dev);
0173     uint8_t reg_val;
0174     int ret = 0;
0175 
0176     mutex_lock(&chip->lock);
0177 
0178     ret = __da903x_read(chip->client, reg, &reg_val);
0179     if (ret)
0180         goto out;
0181 
0182     if ((reg_val & bit_mask) != bit_mask) {
0183         reg_val |= bit_mask;
0184         ret = __da903x_write(chip->client, reg, reg_val);
0185     }
0186 out:
0187     mutex_unlock(&chip->lock);
0188     return ret;
0189 }
0190 EXPORT_SYMBOL_GPL(da903x_set_bits);
0191 
0192 int da903x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
0193 {
0194     struct da903x_chip *chip = dev_get_drvdata(dev);
0195     uint8_t reg_val;
0196     int ret = 0;
0197 
0198     mutex_lock(&chip->lock);
0199 
0200     ret = __da903x_read(chip->client, reg, &reg_val);
0201     if (ret)
0202         goto out;
0203 
0204     if (reg_val & bit_mask) {
0205         reg_val &= ~bit_mask;
0206         ret = __da903x_write(chip->client, reg, reg_val);
0207     }
0208 out:
0209     mutex_unlock(&chip->lock);
0210     return ret;
0211 }
0212 EXPORT_SYMBOL_GPL(da903x_clr_bits);
0213 
0214 int da903x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
0215 {
0216     struct da903x_chip *chip = dev_get_drvdata(dev);
0217     uint8_t reg_val;
0218     int ret = 0;
0219 
0220     mutex_lock(&chip->lock);
0221 
0222     ret = __da903x_read(chip->client, reg, &reg_val);
0223     if (ret)
0224         goto out;
0225 
0226     if ((reg_val & mask) != val) {
0227         reg_val = (reg_val & ~mask) | val;
0228         ret = __da903x_write(chip->client, reg, reg_val);
0229     }
0230 out:
0231     mutex_unlock(&chip->lock);
0232     return ret;
0233 }
0234 EXPORT_SYMBOL_GPL(da903x_update);
0235 
0236 int da903x_query_status(struct device *dev, unsigned int sbits)
0237 {
0238     struct da903x_chip *chip = dev_get_drvdata(dev);
0239     unsigned int status = 0;
0240 
0241     chip->ops->read_status(chip, &status);
0242     return ((status & sbits) == sbits);
0243 }
0244 EXPORT_SYMBOL(da903x_query_status);
0245 
0246 static int da9030_init_chip(struct da903x_chip *chip)
0247 {
0248     uint8_t chip_id;
0249     int err;
0250 
0251     err = __da903x_read(chip->client, DA9030_CHIP_ID, &chip_id);
0252     if (err)
0253         return err;
0254 
0255     err = __da903x_write(chip->client, DA9030_SYS_CTRL_A, 0xE8);
0256     if (err)
0257         return err;
0258 
0259     dev_info(chip->dev, "DA9030 (CHIP ID: 0x%02x) detected\n", chip_id);
0260     return 0;
0261 }
0262 
0263 static int da9030_unmask_events(struct da903x_chip *chip, unsigned int events)
0264 {
0265     uint8_t v[3];
0266 
0267     chip->events_mask &= ~events;
0268 
0269     v[0] = (chip->events_mask & 0xff);
0270     v[1] = (chip->events_mask >> 8) & 0xff;
0271     v[2] = (chip->events_mask >> 16) & 0xff;
0272 
0273     return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
0274 }
0275 
0276 static int da9030_mask_events(struct da903x_chip *chip, unsigned int events)
0277 {
0278     uint8_t v[3];
0279 
0280     chip->events_mask |= events;
0281 
0282     v[0] = (chip->events_mask & 0xff);
0283     v[1] = (chip->events_mask >> 8) & 0xff;
0284     v[2] = (chip->events_mask >> 16) & 0xff;
0285 
0286     return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
0287 }
0288 
0289 static int da9030_read_events(struct da903x_chip *chip, unsigned int *events)
0290 {
0291     uint8_t v[3] = {0, 0, 0};
0292     int ret;
0293 
0294     ret = __da903x_reads(chip->client, DA9030_EVENT_A, 3, v);
0295     if (ret < 0)
0296         return ret;
0297 
0298     *events = (v[2] << 16) | (v[1] << 8) | v[0];
0299     return 0;
0300 }
0301 
0302 static int da9030_read_status(struct da903x_chip *chip, unsigned int *status)
0303 {
0304     return __da903x_read(chip->client, DA9030_STATUS, (uint8_t *)status);
0305 }
0306 
0307 static int da9034_init_chip(struct da903x_chip *chip)
0308 {
0309     uint8_t chip_id;
0310     int err;
0311 
0312     err = __da903x_read(chip->client, DA9034_CHIP_ID, &chip_id);
0313     if (err)
0314         return err;
0315 
0316     err = __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0xE8);
0317     if (err)
0318         return err;
0319 
0320     /* avoid SRAM power off during sleep*/
0321     __da903x_write(chip->client, 0x10, 0x07);
0322     __da903x_write(chip->client, 0x11, 0xff);
0323     __da903x_write(chip->client, 0x12, 0xff);
0324 
0325     /* Enable the ONKEY power down functionality */
0326     __da903x_write(chip->client, DA9034_SYS_CTRL_B, 0x20);
0327     __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0x60);
0328 
0329     /* workaround to make LEDs work */
0330     __da903x_write(chip->client, 0x90, 0x01);
0331     __da903x_write(chip->client, 0xB0, 0x08);
0332 
0333     /* make ADTV1 and SDTV1 effective */
0334     __da903x_write(chip->client, 0x20, 0x00);
0335 
0336     dev_info(chip->dev, "DA9034 (CHIP ID: 0x%02x) detected\n", chip_id);
0337     return 0;
0338 }
0339 
0340 static int da9034_unmask_events(struct da903x_chip *chip, unsigned int events)
0341 {
0342     uint8_t v[4];
0343 
0344     chip->events_mask &= ~events;
0345 
0346     v[0] = (chip->events_mask & 0xff);
0347     v[1] = (chip->events_mask >> 8) & 0xff;
0348     v[2] = (chip->events_mask >> 16) & 0xff;
0349     v[3] = (chip->events_mask >> 24) & 0xff;
0350 
0351     return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
0352 }
0353 
0354 static int da9034_mask_events(struct da903x_chip *chip, unsigned int events)
0355 {
0356     uint8_t v[4];
0357 
0358     chip->events_mask |= events;
0359 
0360     v[0] = (chip->events_mask & 0xff);
0361     v[1] = (chip->events_mask >> 8) & 0xff;
0362     v[2] = (chip->events_mask >> 16) & 0xff;
0363     v[3] = (chip->events_mask >> 24) & 0xff;
0364 
0365     return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
0366 }
0367 
0368 static int da9034_read_events(struct da903x_chip *chip, unsigned int *events)
0369 {
0370     uint8_t v[4] = {0, 0, 0, 0};
0371     int ret;
0372 
0373     ret = __da903x_reads(chip->client, DA9034_EVENT_A, 4, v);
0374     if (ret < 0)
0375         return ret;
0376 
0377     *events = (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | v[0];
0378     return 0;
0379 }
0380 
0381 static int da9034_read_status(struct da903x_chip *chip, unsigned int *status)
0382 {
0383     uint8_t v[2] = {0, 0};
0384     int ret = 0;
0385 
0386     ret = __da903x_reads(chip->client, DA9034_STATUS_A, 2, v);
0387     if (ret)
0388         return ret;
0389 
0390     *status = (v[1] << 8) | v[0];
0391     return 0;
0392 }
0393 
0394 static void da903x_irq_work(struct work_struct *work)
0395 {
0396     struct da903x_chip *chip =
0397         container_of(work, struct da903x_chip, irq_work);
0398     unsigned int events = 0;
0399 
0400     while (1) {
0401         if (chip->ops->read_events(chip, &events))
0402             break;
0403 
0404         events &= ~chip->events_mask;
0405         if (events == 0)
0406             break;
0407 
0408         blocking_notifier_call_chain(
0409                 &chip->notifier_list, events, NULL);
0410     }
0411     enable_irq(chip->client->irq);
0412 }
0413 
0414 static irqreturn_t da903x_irq_handler(int irq, void *data)
0415 {
0416     struct da903x_chip *chip = data;
0417 
0418     disable_irq_nosync(irq);
0419     (void)schedule_work(&chip->irq_work);
0420 
0421     return IRQ_HANDLED;
0422 }
0423 
0424 static const struct da903x_chip_ops da903x_ops[] = {
0425     [0] = {
0426         .init_chip  = da9030_init_chip,
0427         .unmask_events  = da9030_unmask_events,
0428         .mask_events    = da9030_mask_events,
0429         .read_events    = da9030_read_events,
0430         .read_status    = da9030_read_status,
0431     },
0432     [1] = {
0433         .init_chip  = da9034_init_chip,
0434         .unmask_events  = da9034_unmask_events,
0435         .mask_events    = da9034_mask_events,
0436         .read_events    = da9034_read_events,
0437         .read_status    = da9034_read_status,
0438     }
0439 };
0440 
0441 static const struct i2c_device_id da903x_id_table[] = {
0442     { "da9030", 0 },
0443     { "da9034", 1 },
0444     { },
0445 };
0446 MODULE_DEVICE_TABLE(i2c, da903x_id_table);
0447 
0448 static int __remove_subdev(struct device *dev, void *unused)
0449 {
0450     platform_device_unregister(to_platform_device(dev));
0451     return 0;
0452 }
0453 
0454 static int da903x_remove_subdevs(struct da903x_chip *chip)
0455 {
0456     return device_for_each_child(chip->dev, NULL, __remove_subdev);
0457 }
0458 
0459 static int da903x_add_subdevs(struct da903x_chip *chip,
0460                     struct da903x_platform_data *pdata)
0461 {
0462     struct da903x_subdev_info *subdev;
0463     struct platform_device *pdev;
0464     int i, ret = 0;
0465 
0466     for (i = 0; i < pdata->num_subdevs; i++) {
0467         subdev = &pdata->subdevs[i];
0468 
0469         pdev = platform_device_alloc(subdev->name, subdev->id);
0470         if (!pdev) {
0471             ret = -ENOMEM;
0472             goto failed;
0473         }
0474 
0475         pdev->dev.parent = chip->dev;
0476         pdev->dev.platform_data = subdev->platform_data;
0477 
0478         ret = platform_device_add(pdev);
0479         if (ret) {
0480             platform_device_put(pdev);
0481             goto failed;
0482         }
0483     }
0484     return 0;
0485 
0486 failed:
0487     da903x_remove_subdevs(chip);
0488     return ret;
0489 }
0490 
0491 static int da903x_probe(struct i2c_client *client,
0492                   const struct i2c_device_id *id)
0493 {
0494     struct da903x_platform_data *pdata = dev_get_platdata(&client->dev);
0495     struct da903x_chip *chip;
0496     unsigned int tmp;
0497     int ret;
0498 
0499     chip = devm_kzalloc(&client->dev, sizeof(struct da903x_chip),
0500                 GFP_KERNEL);
0501     if (chip == NULL)
0502         return -ENOMEM;
0503 
0504     chip->client = client;
0505     chip->dev = &client->dev;
0506     chip->ops = &da903x_ops[id->driver_data];
0507 
0508     mutex_init(&chip->lock);
0509     INIT_WORK(&chip->irq_work, da903x_irq_work);
0510     BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
0511 
0512     i2c_set_clientdata(client, chip);
0513 
0514     ret = chip->ops->init_chip(chip);
0515     if (ret)
0516         return ret;
0517 
0518     /* mask and clear all IRQs */
0519     chip->events_mask = 0xffffffff;
0520     chip->ops->mask_events(chip, chip->events_mask);
0521     chip->ops->read_events(chip, &tmp);
0522 
0523     ret = devm_request_irq(&client->dev, client->irq, da903x_irq_handler,
0524             IRQF_TRIGGER_FALLING,
0525             "da903x", chip);
0526     if (ret) {
0527         dev_err(&client->dev, "failed to request irq %d\n",
0528                 client->irq);
0529         return ret;
0530     }
0531 
0532     return da903x_add_subdevs(chip, pdata);
0533 }
0534 
0535 static int da903x_remove(struct i2c_client *client)
0536 {
0537     struct da903x_chip *chip = i2c_get_clientdata(client);
0538 
0539     da903x_remove_subdevs(chip);
0540     return 0;
0541 }
0542 
0543 static struct i2c_driver da903x_driver = {
0544     .driver = {
0545         .name   = "da903x",
0546     },
0547     .probe      = da903x_probe,
0548     .remove     = da903x_remove,
0549     .id_table   = da903x_id_table,
0550 };
0551 
0552 static int __init da903x_init(void)
0553 {
0554     return i2c_add_driver(&da903x_driver);
0555 }
0556 subsys_initcall(da903x_init);
0557 
0558 static void __exit da903x_exit(void)
0559 {
0560     i2c_del_driver(&da903x_driver);
0561 }
0562 module_exit(da903x_exit);
0563 
0564 MODULE_DESCRIPTION("PMIC Driver for Dialog Semiconductor DA9034");
0565 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
0566 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
0567 MODULE_LICENSE("GPL v2");