leds-lp55xx-common.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * LP5521/LP5523/LP55231 Common Driver
  3. *
  4. * Copyright 2012 Texas Instruments
  5. *
  6. * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Derived from leds-lp5521.c, leds-lp5523.c
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/i2c.h>
  16. #include <linux/leds.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_data/leds-lp55xx.h>
  19. #include "leds-lp55xx-common.h"
  20. static void lp55xx_reset_device(struct lp55xx_chip *chip)
  21. {
  22. struct lp55xx_device_config *cfg = chip->cfg;
  23. u8 addr = cfg->reset.addr;
  24. u8 val = cfg->reset.val;
  25. /* no error checking here because no ACK from the device after reset */
  26. lp55xx_write(chip, addr, val);
  27. }
  28. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  29. {
  30. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  31. }
  32. EXPORT_SYMBOL_GPL(lp55xx_write);
  33. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  34. {
  35. s32 ret;
  36. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  37. if (ret < 0)
  38. return ret;
  39. *val = ret;
  40. return 0;
  41. }
  42. EXPORT_SYMBOL_GPL(lp55xx_read);
  43. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  44. {
  45. int ret;
  46. u8 tmp;
  47. ret = lp55xx_read(chip, reg, &tmp);
  48. if (ret)
  49. return ret;
  50. tmp &= ~mask;
  51. tmp |= val & mask;
  52. return lp55xx_write(chip, reg, tmp);
  53. }
  54. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  55. int lp55xx_init_device(struct lp55xx_chip *chip)
  56. {
  57. struct lp55xx_platform_data *pdata;
  58. struct lp55xx_device_config *cfg;
  59. struct device *dev = &chip->cl->dev;
  60. int ret = 0;
  61. WARN_ON(!chip);
  62. pdata = chip->pdata;
  63. cfg = chip->cfg;
  64. if (!pdata || !cfg)
  65. return -EINVAL;
  66. if (pdata->setup_resources) {
  67. ret = pdata->setup_resources();
  68. if (ret < 0) {
  69. dev_err(dev, "setup resoure err: %d\n", ret);
  70. goto err;
  71. }
  72. }
  73. if (pdata->enable) {
  74. pdata->enable(0);
  75. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  76. pdata->enable(1);
  77. usleep_range(1000, 2000); /* 500us abs min. */
  78. }
  79. lp55xx_reset_device(chip);
  80. /*
  81. * Exact value is not available. 10 - 20ms
  82. * appears to be enough for reset.
  83. */
  84. usleep_range(10000, 20000);
  85. err:
  86. return ret;
  87. }
  88. EXPORT_SYMBOL_GPL(lp55xx_init_device);
  89. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  90. MODULE_DESCRIPTION("LP55xx Common Driver");
  91. MODULE_LICENSE("GPL");