adp5520.c 8.6 KB

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