adp5520.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Base driver for Analog Devices ADP5520/ADP5501 MFD PMICs
  3. * LCD Backlight: drivers/video/backlight/adp5520_bl
  4. * LEDs : drivers/led/leds-adp5520
  5. * GPIO : drivers/gpio/adp5520-gpio (ADP5520 only)
  6. * Keys : drivers/input/keyboard/adp5520-keys (ADP5520 only)
  7. *
  8. * Copyright 2009 Analog Devices Inc.
  9. *
  10. * Derived from da903x:
  11. * Copyright (C) 2008 Compulab, Ltd.
  12. * Mike Rapoport <mike@compulab.co.il>
  13. *
  14. * Copyright (C) 2006-2008 Marvell International Ltd.
  15. * Eric Miao <eric.miao@marvell.com>
  16. *
  17. * Licensed under the GPL-2 or later.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/err.h>
  26. #include <linux/i2c.h>
  27. #include <linux/mfd/adp5520.h>
  28. struct adp5520_chip {
  29. struct i2c_client *client;
  30. struct device *dev;
  31. struct mutex lock;
  32. struct blocking_notifier_head notifier_list;
  33. int irq;
  34. unsigned long id;
  35. uint8_t mode;
  36. };
  37. static int __adp5520_read(struct i2c_client *client,
  38. int reg, uint8_t *val)
  39. {
  40. int ret;
  41. ret = i2c_smbus_read_byte_data(client, reg);
  42. if (ret < 0) {
  43. dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
  44. return ret;
  45. }
  46. *val = (uint8_t)ret;
  47. return 0;
  48. }
  49. static int __adp5520_write(struct i2c_client *client,
  50. int reg, uint8_t val)
  51. {
  52. int ret;
  53. ret = i2c_smbus_write_byte_data(client, reg, val);
  54. if (ret < 0) {
  55. dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
  56. val, reg);
  57. return ret;
  58. }
  59. return 0;
  60. }
  61. static int __adp5520_ack_bits(struct i2c_client *client, int reg,
  62. uint8_t bit_mask)
  63. {
  64. struct adp5520_chip *chip = i2c_get_clientdata(client);
  65. uint8_t reg_val;
  66. int ret;
  67. mutex_lock(&chip->lock);
  68. ret = __adp5520_read(client, reg, &reg_val);
  69. if (!ret) {
  70. reg_val |= bit_mask;
  71. ret = __adp5520_write(client, reg, reg_val);
  72. }
  73. mutex_unlock(&chip->lock);
  74. return ret;
  75. }
  76. int adp5520_write(struct device *dev, int reg, uint8_t val)
  77. {
  78. return __adp5520_write(to_i2c_client(dev), reg, val);
  79. }
  80. EXPORT_SYMBOL_GPL(adp5520_write);
  81. int adp5520_read(struct device *dev, int reg, uint8_t *val)
  82. {
  83. return __adp5520_read(to_i2c_client(dev), reg, val);
  84. }
  85. EXPORT_SYMBOL_GPL(adp5520_read);
  86. int adp5520_set_bits(struct device *dev, int reg, uint8_t bit_mask)
  87. {
  88. struct adp5520_chip *chip = dev_get_drvdata(dev);
  89. uint8_t reg_val;
  90. int ret;
  91. mutex_lock(&chip->lock);
  92. ret = __adp5520_read(chip->client, reg, &reg_val);
  93. if (!ret && ((reg_val & bit_mask) != bit_mask)) {
  94. reg_val |= bit_mask;
  95. ret = __adp5520_write(chip->client, reg, reg_val);
  96. }
  97. mutex_unlock(&chip->lock);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(adp5520_set_bits);
  101. int adp5520_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
  102. {
  103. struct adp5520_chip *chip = dev_get_drvdata(dev);
  104. uint8_t reg_val;
  105. int ret;
  106. mutex_lock(&chip->lock);
  107. ret = __adp5520_read(chip->client, reg, &reg_val);
  108. if (!ret && (reg_val & bit_mask)) {
  109. reg_val &= ~bit_mask;
  110. ret = __adp5520_write(chip->client, reg, reg_val);
  111. }
  112. mutex_unlock(&chip->lock);
  113. return ret;
  114. }
  115. EXPORT_SYMBOL_GPL(adp5520_clr_bits);
  116. int adp5520_register_notifier(struct device *dev, struct notifier_block *nb,
  117. unsigned int events)
  118. {
  119. struct adp5520_chip *chip = dev_get_drvdata(dev);
  120. if (chip->irq) {
  121. adp5520_set_bits(chip->dev, ADP5520_INTERRUPT_ENABLE,
  122. events & (ADP5520_KP_IEN | ADP5520_KR_IEN |
  123. ADP5520_OVP_IEN | ADP5520_CMPR_IEN));
  124. return blocking_notifier_chain_register(&chip->notifier_list,
  125. nb);
  126. }
  127. return -ENODEV;
  128. }
  129. EXPORT_SYMBOL_GPL(adp5520_register_notifier);
  130. int adp5520_unregister_notifier(struct device *dev, struct notifier_block *nb,
  131. unsigned int events)
  132. {
  133. struct adp5520_chip *chip = dev_get_drvdata(dev);
  134. adp5520_clr_bits(chip->dev, ADP5520_INTERRUPT_ENABLE,
  135. events & (ADP5520_KP_IEN | ADP5520_KR_IEN |
  136. ADP5520_OVP_IEN | ADP5520_CMPR_IEN));
  137. return blocking_notifier_chain_unregister(&chip->notifier_list, nb);
  138. }
  139. EXPORT_SYMBOL_GPL(adp5520_unregister_notifier);
  140. static irqreturn_t adp5520_irq_thread(int irq, void *data)
  141. {
  142. struct adp5520_chip *chip = data;
  143. unsigned int events;
  144. uint8_t reg_val;
  145. int ret;
  146. ret = __adp5520_read(chip->client, ADP5520_MODE_STATUS, &reg_val);
  147. if (ret)
  148. goto out;
  149. events = reg_val & (ADP5520_OVP_INT | ADP5520_CMPR_INT |
  150. ADP5520_GPI_INT | ADP5520_KR_INT | ADP5520_KP_INT);
  151. blocking_notifier_call_chain(&chip->notifier_list, events, NULL);
  152. /* ACK, Sticky bits are W1C */
  153. __adp5520_ack_bits(chip->client, ADP5520_MODE_STATUS, events);
  154. out:
  155. return IRQ_HANDLED;
  156. }
  157. static int __remove_subdev(struct device *dev, void *unused)
  158. {
  159. platform_device_unregister(to_platform_device(dev));
  160. return 0;
  161. }
  162. static int adp5520_remove_subdevs(struct adp5520_chip *chip)
  163. {
  164. return device_for_each_child(chip->dev, NULL, __remove_subdev);
  165. }
  166. static int adp5520_probe(struct i2c_client *client,
  167. const struct i2c_device_id *id)
  168. {
  169. struct adp5520_platform_data *pdata = dev_get_platdata(&client->dev);
  170. struct platform_device *pdev;
  171. struct adp5520_chip *chip;
  172. int ret;
  173. if (!i2c_check_functionality(client->adapter,
  174. I2C_FUNC_SMBUS_BYTE_DATA)) {
  175. dev_err(&client->dev, "SMBUS Word Data not Supported\n");
  176. return -EIO;
  177. }
  178. if (pdata == NULL) {
  179. dev_err(&client->dev, "missing platform data\n");
  180. return -ENODEV;
  181. }
  182. chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
  183. if (!chip)
  184. return -ENOMEM;
  185. i2c_set_clientdata(client, chip);
  186. chip->client = client;
  187. chip->dev = &client->dev;
  188. chip->irq = client->irq;
  189. chip->id = id->driver_data;
  190. mutex_init(&chip->lock);
  191. if (chip->irq) {
  192. BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
  193. ret = request_threaded_irq(chip->irq, NULL, adp5520_irq_thread,
  194. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  195. "adp5520", chip);
  196. if (ret) {
  197. dev_err(&client->dev, "failed to request irq %d\n",
  198. chip->irq);
  199. return ret;
  200. }
  201. }
  202. ret = adp5520_write(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY);
  203. if (ret) {
  204. dev_err(&client->dev, "failed to write\n");
  205. goto out_free_irq;
  206. }
  207. if (pdata->keys) {
  208. pdev = platform_device_register_data(chip->dev, "adp5520-keys",
  209. chip->id, pdata->keys, sizeof(*pdata->keys));
  210. if (IS_ERR(pdev)) {
  211. ret = PTR_ERR(pdev);
  212. goto out_remove_subdevs;
  213. }
  214. }
  215. if (pdata->gpio) {
  216. pdev = platform_device_register_data(chip->dev, "adp5520-gpio",
  217. chip->id, pdata->gpio, sizeof(*pdata->gpio));
  218. if (IS_ERR(pdev)) {
  219. ret = PTR_ERR(pdev);
  220. goto out_remove_subdevs;
  221. }
  222. }
  223. if (pdata->leds) {
  224. pdev = platform_device_register_data(chip->dev, "adp5520-led",
  225. chip->id, pdata->leds, sizeof(*pdata->leds));
  226. if (IS_ERR(pdev)) {
  227. ret = PTR_ERR(pdev);
  228. goto out_remove_subdevs;
  229. }
  230. }
  231. if (pdata->backlight) {
  232. pdev = platform_device_register_data(chip->dev,
  233. "adp5520-backlight",
  234. chip->id,
  235. pdata->backlight,
  236. sizeof(*pdata->backlight));
  237. if (IS_ERR(pdev)) {
  238. ret = PTR_ERR(pdev);
  239. goto out_remove_subdevs;
  240. }
  241. }
  242. return 0;
  243. out_remove_subdevs:
  244. adp5520_remove_subdevs(chip);
  245. out_free_irq:
  246. if (chip->irq)
  247. free_irq(chip->irq, chip);
  248. return ret;
  249. }
  250. static int adp5520_remove(struct i2c_client *client)
  251. {
  252. struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
  253. if (chip->irq)
  254. free_irq(chip->irq, chip);
  255. adp5520_remove_subdevs(chip);
  256. adp5520_write(chip->dev, ADP5520_MODE_STATUS, 0);
  257. return 0;
  258. }
  259. #ifdef CONFIG_PM_SLEEP
  260. static int adp5520_suspend(struct device *dev)
  261. {
  262. struct i2c_client *client = to_i2c_client(dev);
  263. struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
  264. adp5520_read(chip->dev, ADP5520_MODE_STATUS, &chip->mode);
  265. /* All other bits are W1C */
  266. chip->mode &= ADP5520_BL_EN | ADP5520_DIM_EN | ADP5520_nSTNBY;
  267. adp5520_write(chip->dev, ADP5520_MODE_STATUS, 0);
  268. return 0;
  269. }
  270. static int adp5520_resume(struct device *dev)
  271. {
  272. struct i2c_client *client = to_i2c_client(dev);
  273. struct adp5520_chip *chip = dev_get_drvdata(&client->dev);
  274. adp5520_write(chip->dev, ADP5520_MODE_STATUS, chip->mode);
  275. return 0;
  276. }
  277. #endif
  278. static SIMPLE_DEV_PM_OPS(adp5520_pm, adp5520_suspend, adp5520_resume);
  279. static const struct i2c_device_id adp5520_id[] = {
  280. { "pmic-adp5520", ID_ADP5520 },
  281. { "pmic-adp5501", ID_ADP5501 },
  282. { }
  283. };
  284. MODULE_DEVICE_TABLE(i2c, adp5520_id);
  285. static struct i2c_driver adp5520_driver = {
  286. .driver = {
  287. .name = "adp5520",
  288. .pm = &adp5520_pm,
  289. },
  290. .probe = adp5520_probe,
  291. .remove = adp5520_remove,
  292. .id_table = adp5520_id,
  293. };
  294. module_i2c_driver(adp5520_driver);
  295. MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
  296. MODULE_DESCRIPTION("ADP5520(01) PMIC-MFD Driver");
  297. MODULE_LICENSE("GPL");