linux/drivers/mfd/da903x.c
<<
>>
Prefs
   1/*
   2 * Base driver for Dialog Semiconductor DA9030/DA9034
   3 *
   4 * Copyright (C) 2008 Compulab, Ltd.
   5 *      Mike Rapoport <mike@compulab.co.il>
   6 *
   7 * Copyright (C) 2006-2008 Marvell International Ltd.
   8 *      Eric Miao <eric.miao@marvell.com>
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of the GNU General Public License version 2 as
  12 * published by the Free Software Foundation.
  13 */
  14
  15#include <linux/kernel.h>
  16#include <linux/module.h>
  17#include <linux/interrupt.h>
  18#include <linux/platform_device.h>
  19#include <linux/i2c.h>
  20#include <linux/mfd/da903x.h>
  21
  22#define DA9030_CHIP_ID          0x00
  23#define DA9030_EVENT_A          0x01
  24#define DA9030_EVENT_B          0x02
  25#define DA9030_EVENT_C          0x03
  26#define DA9030_STATUS           0x04
  27#define DA9030_IRQ_MASK_A       0x05
  28#define DA9030_IRQ_MASK_B       0x06
  29#define DA9030_IRQ_MASK_C       0x07
  30#define DA9030_SYS_CTRL_A       0x08
  31#define DA9030_SYS_CTRL_B       0x09
  32#define DA9030_FAULT_LOG        0x0a
  33
  34#define DA9034_CHIP_ID          0x00
  35#define DA9034_EVENT_A          0x01
  36#define DA9034_EVENT_B          0x02
  37#define DA9034_EVENT_C          0x03
  38#define DA9034_EVENT_D          0x04
  39#define DA9034_STATUS_A         0x05
  40#define DA9034_STATUS_B         0x06
  41#define DA9034_IRQ_MASK_A       0x07
  42#define DA9034_IRQ_MASK_B       0x08
  43#define DA9034_IRQ_MASK_C       0x09
  44#define DA9034_IRQ_MASK_D       0x0a
  45#define DA9034_SYS_CTRL_A       0x0b
  46#define DA9034_SYS_CTRL_B       0x0c
  47#define DA9034_FAULT_LOG        0x0d
  48
  49struct da903x_chip;
  50
  51struct da903x_chip_ops {
  52        int     (*init_chip)(struct da903x_chip *);
  53        int     (*unmask_events)(struct da903x_chip *, unsigned int events);
  54        int     (*mask_events)(struct da903x_chip *, unsigned int events);
  55        int     (*read_events)(struct da903x_chip *, unsigned int *events);
  56        int     (*read_status)(struct da903x_chip *, unsigned int *status);
  57};
  58
  59struct da903x_chip {
  60        struct i2c_client       *client;
  61        struct device           *dev;
  62        struct da903x_chip_ops  *ops;
  63
  64        int                     type;
  65        uint32_t                events_mask;
  66
  67        struct mutex            lock;
  68        struct work_struct      irq_work;
  69
  70        struct blocking_notifier_head notifier_list;
  71};
  72
  73static inline int __da903x_read(struct i2c_client *client,
  74                                int reg, uint8_t *val)
  75{
  76        int ret;
  77
  78        ret = i2c_smbus_read_byte_data(client, reg);
  79        if (ret < 0) {
  80                dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
  81                return ret;
  82        }
  83
  84        *val = (uint8_t)ret;
  85        return 0;
  86}
  87
  88static inline int __da903x_reads(struct i2c_client *client, int reg,
  89                                 int len, uint8_t *val)
  90{
  91        int ret;
  92
  93        ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
  94        if (ret < 0) {
  95                dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
  96                return ret;
  97        }
  98        return 0;
  99}
 100
 101static inline int __da903x_write(struct i2c_client *client,
 102                                 int reg, uint8_t val)
 103{
 104        int ret;
 105
 106        ret = i2c_smbus_write_byte_data(client, reg, val);
 107        if (ret < 0) {
 108                dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
 109                                val, reg);
 110                return ret;
 111        }
 112        return 0;
 113}
 114
 115static inline int __da903x_writes(struct i2c_client *client, int reg,
 116                                  int len, uint8_t *val)
 117{
 118        int ret;
 119
 120        ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
 121        if (ret < 0) {
 122                dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
 123                return ret;
 124        }
 125        return 0;
 126}
 127
 128int da903x_register_notifier(struct device *dev, struct notifier_block *nb,
 129                                unsigned int events)
 130{
 131        struct da903x_chip *chip = dev_get_drvdata(dev);
 132
 133        chip->ops->unmask_events(chip, events);
 134        return blocking_notifier_chain_register(&chip->notifier_list, nb);
 135}
 136EXPORT_SYMBOL_GPL(da903x_register_notifier);
 137
 138int da903x_unregister_notifier(struct device *dev, struct notifier_block *nb,
 139                                unsigned int events)
 140{
 141        struct da903x_chip *chip = dev_get_drvdata(dev);
 142
 143        chip->ops->mask_events(chip, events);
 144        return blocking_notifier_chain_unregister(&chip->notifier_list, nb);
 145}
 146EXPORT_SYMBOL_GPL(da903x_unregister_notifier);
 147
 148int da903x_write(struct device *dev, int reg, uint8_t val)
 149{
 150        return __da903x_write(to_i2c_client(dev), reg, val);
 151}
 152EXPORT_SYMBOL_GPL(da903x_write);
 153
 154int da903x_writes(struct device *dev, int reg, int len, uint8_t *val)
 155{
 156        return __da903x_writes(to_i2c_client(dev), reg, len, val);
 157}
 158EXPORT_SYMBOL_GPL(da903x_writes);
 159
 160int da903x_read(struct device *dev, int reg, uint8_t *val)
 161{
 162        return __da903x_read(to_i2c_client(dev), reg, val);
 163}
 164EXPORT_SYMBOL_GPL(da903x_read);
 165
 166int da903x_reads(struct device *dev, int reg, int len, uint8_t *val)
 167{
 168        return __da903x_reads(to_i2c_client(dev), reg, len, val);
 169}
 170EXPORT_SYMBOL_GPL(da903x_reads);
 171
 172int da903x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
 173{
 174        struct da903x_chip *chip = dev_get_drvdata(dev);
 175        uint8_t reg_val;
 176        int ret = 0;
 177
 178        mutex_lock(&chip->lock);
 179
 180        ret = __da903x_read(chip->client, reg, &reg_val);
 181        if (ret)
 182                goto out;
 183
 184        if ((reg_val & bit_mask) == 0) {
 185                reg_val |= bit_mask;
 186                ret = __da903x_write(chip->client, reg, reg_val);
 187        }
 188out:
 189        mutex_unlock(&chip->lock);
 190        return ret;
 191}
 192EXPORT_SYMBOL_GPL(da903x_set_bits);
 193
 194int da903x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
 195{
 196        struct da903x_chip *chip = dev_get_drvdata(dev);
 197        uint8_t reg_val;
 198        int ret = 0;
 199
 200        mutex_lock(&chip->lock);
 201
 202        ret = __da903x_read(chip->client, reg, &reg_val);
 203        if (ret)
 204                goto out;
 205
 206        if (reg_val & bit_mask) {
 207                reg_val &= ~bit_mask;
 208                ret = __da903x_write(chip->client, reg, reg_val);
 209        }
 210out:
 211        mutex_unlock(&chip->lock);
 212        return ret;
 213}
 214EXPORT_SYMBOL_GPL(da903x_clr_bits);
 215
 216int da903x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
 217{
 218        struct da903x_chip *chip = dev_get_drvdata(dev);
 219        uint8_t reg_val;
 220        int ret = 0;
 221
 222        mutex_lock(&chip->lock);
 223
 224        ret = __da903x_read(chip->client, reg, &reg_val);
 225        if (ret)
 226                goto out;
 227
 228        if ((reg_val & mask) != val) {
 229                reg_val = (reg_val & ~mask) | val;
 230                ret = __da903x_write(chip->client, reg, reg_val);
 231        }
 232out:
 233        mutex_unlock(&chip->lock);
 234        return ret;
 235}
 236EXPORT_SYMBOL_GPL(da903x_update);
 237
 238int da903x_query_status(struct device *dev, unsigned int sbits)
 239{
 240        struct da903x_chip *chip = dev_get_drvdata(dev);
 241        unsigned int status = 0;
 242
 243        chip->ops->read_status(chip, &status);
 244        return ((status & sbits) == sbits);
 245}
 246EXPORT_SYMBOL(da903x_query_status);
 247
 248static int __devinit da9030_init_chip(struct da903x_chip *chip)
 249{
 250        uint8_t chip_id;
 251        int err;
 252
 253        err = __da903x_read(chip->client, DA9030_CHIP_ID, &chip_id);
 254        if (err)
 255                return err;
 256
 257        err = __da903x_write(chip->client, DA9030_SYS_CTRL_A, 0xE8);
 258        if (err)
 259                return err;
 260
 261        dev_info(chip->dev, "DA9030 (CHIP ID: 0x%02x) detected\n", chip_id);
 262        return 0;
 263}
 264
 265static int da9030_unmask_events(struct da903x_chip *chip, unsigned int events)
 266{
 267        uint8_t v[3];
 268
 269        chip->events_mask &= ~events;
 270
 271        v[0] = (chip->events_mask & 0xff);
 272        v[1] = (chip->events_mask >> 8) & 0xff;
 273        v[2] = (chip->events_mask >> 16) & 0xff;
 274
 275        return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
 276}
 277
 278static int da9030_mask_events(struct da903x_chip *chip, unsigned int events)
 279{
 280        uint8_t v[3];
 281
 282        chip->events_mask |= events;
 283
 284        v[0] = (chip->events_mask & 0xff);
 285        v[1] = (chip->events_mask >> 8) & 0xff;
 286        v[2] = (chip->events_mask >> 16) & 0xff;
 287
 288        return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
 289}
 290
 291static int da9030_read_events(struct da903x_chip *chip, unsigned int *events)
 292{
 293        uint8_t v[3] = {0, 0, 0};
 294        int ret;
 295
 296        ret = __da903x_reads(chip->client, DA9030_EVENT_A, 3, v);
 297        if (ret < 0)
 298                return ret;
 299
 300        *events = (v[2] << 16) | (v[1] << 8) | v[0];
 301        return 0;
 302}
 303
 304static int da9030_read_status(struct da903x_chip *chip, unsigned int *status)
 305{
 306        return __da903x_read(chip->client, DA9030_STATUS, (uint8_t *)status);
 307}
 308
 309static int da9034_init_chip(struct da903x_chip *chip)
 310{
 311        uint8_t chip_id;
 312        int err;
 313
 314        err = __da903x_read(chip->client, DA9034_CHIP_ID, &chip_id);
 315        if (err)
 316                return err;
 317
 318        err = __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0xE8);
 319        if (err)
 320                return err;
 321
 322        /* avoid SRAM power off during sleep*/
 323        __da903x_write(chip->client, 0x10, 0x07);
 324        __da903x_write(chip->client, 0x11, 0xff);
 325        __da903x_write(chip->client, 0x12, 0xff);
 326
 327        /* Enable the ONKEY power down functionality */
 328        __da903x_write(chip->client, DA9034_SYS_CTRL_B, 0x20);
 329        __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0x60);
 330
 331        /* workaround to make LEDs work */
 332        __da903x_write(chip->client, 0x90, 0x01);
 333        __da903x_write(chip->client, 0xB0, 0x08);
 334
 335        /* make ADTV1 and SDTV1 effective */
 336        __da903x_write(chip->client, 0x20, 0x00);
 337
 338        dev_info(chip->dev, "DA9034 (CHIP ID: 0x%02x) detected\n", chip_id);
 339        return 0;
 340}
 341
 342static int da9034_unmask_events(struct da903x_chip *chip, unsigned int events)
 343{
 344        uint8_t v[4];
 345
 346        chip->events_mask &= ~events;
 347
 348        v[0] = (chip->events_mask & 0xff);
 349        v[1] = (chip->events_mask >> 8) & 0xff;
 350        v[2] = (chip->events_mask >> 16) & 0xff;
 351        v[3] = (chip->events_mask >> 24) & 0xff;
 352
 353        return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
 354}
 355
 356static int da9034_mask_events(struct da903x_chip *chip, unsigned int events)
 357{
 358        uint8_t v[4];
 359
 360        chip->events_mask |= events;
 361
 362        v[0] = (chip->events_mask & 0xff);
 363        v[1] = (chip->events_mask >> 8) & 0xff;
 364        v[2] = (chip->events_mask >> 16) & 0xff;
 365        v[3] = (chip->events_mask >> 24) & 0xff;
 366
 367        return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
 368}
 369
 370static int da9034_read_events(struct da903x_chip *chip, unsigned int *events)
 371{
 372        uint8_t v[4] = {0, 0, 0, 0};
 373        int ret;
 374
 375        ret = __da903x_reads(chip->client, DA9034_EVENT_A, 4, v);
 376        if (ret < 0)
 377                return ret;
 378
 379        *events = (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | v[0];
 380        return 0;
 381}
 382
 383static int da9034_read_status(struct da903x_chip *chip, unsigned int *status)
 384{
 385        uint8_t v[2] = {0, 0};
 386        int ret = 0;
 387
 388        ret = __da903x_reads(chip->client, DA9034_STATUS_A, 2, v);
 389        if (ret)
 390                return ret;
 391
 392        *status = (v[1] << 8) | v[0];
 393        return 0;
 394}
 395
 396static void da903x_irq_work(struct work_struct *work)
 397{
 398        struct da903x_chip *chip =
 399                container_of(work, struct da903x_chip, irq_work);
 400        unsigned int events = 0;
 401
 402        while (1) {
 403                if (chip->ops->read_events(chip, &events))
 404                        break;
 405
 406                events &= ~chip->events_mask;
 407                if (events == 0)
 408                        break;
 409
 410                blocking_notifier_call_chain(
 411                                &chip->notifier_list, events, NULL);
 412        }
 413        enable_irq(chip->client->irq);
 414}
 415
 416static irqreturn_t da903x_irq_handler(int irq, void *data)
 417{
 418        struct da903x_chip *chip = data;
 419
 420        disable_irq_nosync(irq);
 421        (void)schedule_work(&chip->irq_work);
 422
 423        return IRQ_HANDLED;
 424}
 425
 426static struct da903x_chip_ops da903x_ops[] = {
 427        [0] = {
 428                .init_chip      = da9030_init_chip,
 429                .unmask_events  = da9030_unmask_events,
 430                .mask_events    = da9030_mask_events,
 431                .read_events    = da9030_read_events,
 432                .read_status    = da9030_read_status,
 433        },
 434        [1] = {
 435                .init_chip      = da9034_init_chip,
 436                .unmask_events  = da9034_unmask_events,
 437                .mask_events    = da9034_mask_events,
 438                .read_events    = da9034_read_events,
 439                .read_status    = da9034_read_status,
 440        }
 441};
 442
 443static const struct i2c_device_id da903x_id_table[] = {
 444        { "da9030", 0 },
 445        { "da9034", 1 },
 446        { },
 447};
 448MODULE_DEVICE_TABLE(i2c, da903x_id_table);
 449
 450static int __remove_subdev(struct device *dev, void *unused)
 451{
 452        platform_device_unregister(to_platform_device(dev));
 453        return 0;
 454}
 455
 456static int da903x_remove_subdevs(struct da903x_chip *chip)
 457{
 458        return device_for_each_child(chip->dev, NULL, __remove_subdev);
 459}
 460
 461static int __devinit da903x_add_subdevs(struct da903x_chip *chip,
 462                                        struct da903x_platform_data *pdata)
 463{
 464        struct da903x_subdev_info *subdev;
 465        struct platform_device *pdev;
 466        int i, ret = 0;
 467
 468        for (i = 0; i < pdata->num_subdevs; i++) {
 469                subdev = &pdata->subdevs[i];
 470
 471                pdev = platform_device_alloc(subdev->name, subdev->id);
 472
 473                pdev->dev.parent = chip->dev;
 474                pdev->dev.platform_data = subdev->platform_data;
 475
 476                ret = platform_device_add(pdev);
 477                if (ret)
 478                        goto failed;
 479        }
 480        return 0;
 481
 482failed:
 483        da903x_remove_subdevs(chip);
 484        return ret;
 485}
 486
 487static int __devinit da903x_probe(struct i2c_client *client,
 488                                  const struct i2c_device_id *id)
 489{
 490        struct da903x_platform_data *pdata = client->dev.platform_data;
 491        struct da903x_chip *chip;
 492        unsigned int tmp;
 493        int ret;
 494
 495        chip = kzalloc(sizeof(struct da903x_chip), GFP_KERNEL);
 496        if (chip == NULL)
 497                return -ENOMEM;
 498
 499        chip->client = client;
 500        chip->dev = &client->dev;
 501        chip->ops = &da903x_ops[id->driver_data];
 502
 503        mutex_init(&chip->lock);
 504        INIT_WORK(&chip->irq_work, da903x_irq_work);
 505        BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
 506
 507        i2c_set_clientdata(client, chip);
 508
 509        ret = chip->ops->init_chip(chip);
 510        if (ret)
 511                goto out_free_chip;
 512
 513        /* mask and clear all IRQs */
 514        chip->events_mask = 0xffffffff;
 515        chip->ops->mask_events(chip, chip->events_mask);
 516        chip->ops->read_events(chip, &tmp);
 517
 518        ret = request_irq(client->irq, da903x_irq_handler,
 519                        IRQF_DISABLED | IRQF_TRIGGER_FALLING,
 520                        "da903x", chip);
 521        if (ret) {
 522                dev_err(&client->dev, "failed to request irq %d\n",
 523                                client->irq);
 524                goto out_free_chip;
 525        }
 526
 527        ret = da903x_add_subdevs(chip, pdata);
 528        if (ret)
 529                goto out_free_irq;
 530
 531        return 0;
 532
 533out_free_irq:
 534        free_irq(client->irq, chip);
 535out_free_chip:
 536        i2c_set_clientdata(client, NULL);
 537        kfree(chip);
 538        return ret;
 539}
 540
 541static int __devexit da903x_remove(struct i2c_client *client)
 542{
 543        struct da903x_chip *chip = i2c_get_clientdata(client);
 544
 545        da903x_remove_subdevs(chip);
 546        kfree(chip);
 547        return 0;
 548}
 549
 550static struct i2c_driver da903x_driver = {
 551        .driver = {
 552                .name   = "da903x",
 553                .owner  = THIS_MODULE,
 554        },
 555        .probe          = da903x_probe,
 556        .remove         = __devexit_p(da903x_remove),
 557        .id_table       = da903x_id_table,
 558};
 559
 560static int __init da903x_init(void)
 561{
 562        return i2c_add_driver(&da903x_driver);
 563}
 564subsys_initcall(da903x_init);
 565
 566static void __exit da903x_exit(void)
 567{
 568        i2c_del_driver(&da903x_driver);
 569}
 570module_exit(da903x_exit);
 571
 572MODULE_DESCRIPTION("PMIC Driver for Dialog Semiconductor DA9034");
 573MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
 574              "Mike Rapoport <mike@compulab.co.il>");
 575MODULE_LICENSE("GPL");
 576