da903x.c 13 KB

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