leds-lp55xx-common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. static int lp55xx_detect_device(struct lp55xx_chip *chip)
  29. {
  30. struct lp55xx_device_config *cfg = chip->cfg;
  31. u8 addr = cfg->enable.addr;
  32. u8 val = cfg->enable.val;
  33. int ret;
  34. ret = lp55xx_write(chip, addr, val);
  35. if (ret)
  36. return ret;
  37. usleep_range(1000, 2000);
  38. ret = lp55xx_read(chip, addr, &val);
  39. if (ret)
  40. return ret;
  41. if (val != cfg->enable.val)
  42. return -ENODEV;
  43. return 0;
  44. }
  45. static int lp55xx_post_init_device(struct lp55xx_chip *chip)
  46. {
  47. struct lp55xx_device_config *cfg = chip->cfg;
  48. if (!cfg->post_init_device)
  49. return 0;
  50. return cfg->post_init_device(chip);
  51. }
  52. int lp55xx_write(struct lp55xx_chip *chip, u8 reg, u8 val)
  53. {
  54. return i2c_smbus_write_byte_data(chip->cl, reg, val);
  55. }
  56. EXPORT_SYMBOL_GPL(lp55xx_write);
  57. int lp55xx_read(struct lp55xx_chip *chip, u8 reg, u8 *val)
  58. {
  59. s32 ret;
  60. ret = i2c_smbus_read_byte_data(chip->cl, reg);
  61. if (ret < 0)
  62. return ret;
  63. *val = ret;
  64. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(lp55xx_read);
  67. int lp55xx_update_bits(struct lp55xx_chip *chip, u8 reg, u8 mask, u8 val)
  68. {
  69. int ret;
  70. u8 tmp;
  71. ret = lp55xx_read(chip, reg, &tmp);
  72. if (ret)
  73. return ret;
  74. tmp &= ~mask;
  75. tmp |= val & mask;
  76. return lp55xx_write(chip, reg, tmp);
  77. }
  78. EXPORT_SYMBOL_GPL(lp55xx_update_bits);
  79. int lp55xx_init_device(struct lp55xx_chip *chip)
  80. {
  81. struct lp55xx_platform_data *pdata;
  82. struct lp55xx_device_config *cfg;
  83. struct device *dev = &chip->cl->dev;
  84. int ret = 0;
  85. WARN_ON(!chip);
  86. pdata = chip->pdata;
  87. cfg = chip->cfg;
  88. if (!pdata || !cfg)
  89. return -EINVAL;
  90. if (pdata->setup_resources) {
  91. ret = pdata->setup_resources();
  92. if (ret < 0) {
  93. dev_err(dev, "setup resoure err: %d\n", ret);
  94. goto err;
  95. }
  96. }
  97. if (pdata->enable) {
  98. pdata->enable(0);
  99. usleep_range(1000, 2000); /* Keep enable down at least 1ms */
  100. pdata->enable(1);
  101. usleep_range(1000, 2000); /* 500us abs min. */
  102. }
  103. lp55xx_reset_device(chip);
  104. /*
  105. * Exact value is not available. 10 - 20ms
  106. * appears to be enough for reset.
  107. */
  108. usleep_range(10000, 20000);
  109. ret = lp55xx_detect_device(chip);
  110. if (ret) {
  111. dev_err(dev, "device detection err: %d\n", ret);
  112. goto err;
  113. }
  114. /* chip specific initialization */
  115. ret = lp55xx_post_init_device(chip);
  116. return 0;
  117. err:
  118. return ret;
  119. }
  120. EXPORT_SYMBOL_GPL(lp55xx_init_device);
  121. MODULE_AUTHOR("Milo Kim <milo.kim@ti.com>");
  122. MODULE_DESCRIPTION("LP55xx Common Driver");
  123. MODULE_LICENSE("GPL");