da9055-core.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Device access for Dialog DA9055 PMICs.
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/input.h>
  16. #include <linux/irq.h>
  17. #include <linux/mutex.h>
  18. #include <linux/mfd/core.h>
  19. #include <linux/mfd/da9055/core.h>
  20. #include <linux/mfd/da9055/pdata.h>
  21. #include <linux/mfd/da9055/reg.h>
  22. #define DA9055_IRQ_NONKEY_MASK 0x01
  23. #define DA9055_IRQ_ALM_MASK 0x02
  24. #define DA9055_IRQ_TICK_MASK 0x04
  25. #define DA9055_IRQ_ADC_MASK 0x08
  26. #define DA9055_IRQ_BUCK_ILIM_MASK 0x08
  27. static bool da9055_register_readable(struct device *dev, unsigned int reg)
  28. {
  29. switch (reg) {
  30. case DA9055_REG_STATUS_A:
  31. case DA9055_REG_STATUS_B:
  32. case DA9055_REG_EVENT_A:
  33. case DA9055_REG_EVENT_B:
  34. case DA9055_REG_EVENT_C:
  35. case DA9055_REG_IRQ_MASK_A:
  36. case DA9055_REG_IRQ_MASK_B:
  37. case DA9055_REG_IRQ_MASK_C:
  38. case DA9055_REG_CONTROL_A:
  39. case DA9055_REG_CONTROL_B:
  40. case DA9055_REG_CONTROL_C:
  41. case DA9055_REG_CONTROL_D:
  42. case DA9055_REG_CONTROL_E:
  43. case DA9055_REG_ADC_MAN:
  44. case DA9055_REG_ADC_CONT:
  45. case DA9055_REG_VSYS_MON:
  46. case DA9055_REG_ADC_RES_L:
  47. case DA9055_REG_ADC_RES_H:
  48. case DA9055_REG_VSYS_RES:
  49. case DA9055_REG_ADCIN1_RES:
  50. case DA9055_REG_ADCIN2_RES:
  51. case DA9055_REG_ADCIN3_RES:
  52. case DA9055_REG_COUNT_S:
  53. case DA9055_REG_COUNT_MI:
  54. case DA9055_REG_COUNT_H:
  55. case DA9055_REG_COUNT_D:
  56. case DA9055_REG_COUNT_MO:
  57. case DA9055_REG_COUNT_Y:
  58. case DA9055_REG_ALARM_H:
  59. case DA9055_REG_ALARM_D:
  60. case DA9055_REG_ALARM_MI:
  61. case DA9055_REG_ALARM_MO:
  62. case DA9055_REG_ALARM_Y:
  63. case DA9055_REG_GPIO0_1:
  64. case DA9055_REG_GPIO2:
  65. case DA9055_REG_GPIO_MODE0_2:
  66. case DA9055_REG_BCORE_CONT:
  67. case DA9055_REG_BMEM_CONT:
  68. case DA9055_REG_LDO1_CONT:
  69. case DA9055_REG_LDO2_CONT:
  70. case DA9055_REG_LDO3_CONT:
  71. case DA9055_REG_LDO4_CONT:
  72. case DA9055_REG_LDO5_CONT:
  73. case DA9055_REG_LDO6_CONT:
  74. case DA9055_REG_BUCK_LIM:
  75. case DA9055_REG_BCORE_MODE:
  76. case DA9055_REG_VBCORE_A:
  77. case DA9055_REG_VBMEM_A:
  78. case DA9055_REG_VLDO1_A:
  79. case DA9055_REG_VLDO2_A:
  80. case DA9055_REG_VLDO3_A:
  81. case DA9055_REG_VLDO4_A:
  82. case DA9055_REG_VLDO5_A:
  83. case DA9055_REG_VLDO6_A:
  84. case DA9055_REG_VBCORE_B:
  85. case DA9055_REG_VBMEM_B:
  86. case DA9055_REG_VLDO1_B:
  87. case DA9055_REG_VLDO2_B:
  88. case DA9055_REG_VLDO3_B:
  89. case DA9055_REG_VLDO4_B:
  90. case DA9055_REG_VLDO5_B:
  91. case DA9055_REG_VLDO6_B:
  92. return true;
  93. default:
  94. return false;
  95. }
  96. }
  97. static bool da9055_register_writeable(struct device *dev, unsigned int reg)
  98. {
  99. switch (reg) {
  100. case DA9055_REG_STATUS_A:
  101. case DA9055_REG_STATUS_B:
  102. case DA9055_REG_EVENT_A:
  103. case DA9055_REG_EVENT_B:
  104. case DA9055_REG_EVENT_C:
  105. case DA9055_REG_IRQ_MASK_A:
  106. case DA9055_REG_IRQ_MASK_B:
  107. case DA9055_REG_IRQ_MASK_C:
  108. case DA9055_REG_CONTROL_A:
  109. case DA9055_REG_CONTROL_B:
  110. case DA9055_REG_CONTROL_C:
  111. case DA9055_REG_CONTROL_D:
  112. case DA9055_REG_CONTROL_E:
  113. case DA9055_REG_ADC_MAN:
  114. case DA9055_REG_ADC_CONT:
  115. case DA9055_REG_VSYS_MON:
  116. case DA9055_REG_ADC_RES_L:
  117. case DA9055_REG_ADC_RES_H:
  118. case DA9055_REG_VSYS_RES:
  119. case DA9055_REG_ADCIN1_RES:
  120. case DA9055_REG_ADCIN2_RES:
  121. case DA9055_REG_ADCIN3_RES:
  122. case DA9055_REG_COUNT_S:
  123. case DA9055_REG_COUNT_MI:
  124. case DA9055_REG_COUNT_H:
  125. case DA9055_REG_COUNT_D:
  126. case DA9055_REG_COUNT_MO:
  127. case DA9055_REG_COUNT_Y:
  128. case DA9055_REG_ALARM_H:
  129. case DA9055_REG_ALARM_D:
  130. case DA9055_REG_ALARM_MI:
  131. case DA9055_REG_ALARM_MO:
  132. case DA9055_REG_ALARM_Y:
  133. case DA9055_REG_GPIO0_1:
  134. case DA9055_REG_GPIO2:
  135. case DA9055_REG_GPIO_MODE0_2:
  136. case DA9055_REG_BCORE_CONT:
  137. case DA9055_REG_BMEM_CONT:
  138. case DA9055_REG_LDO1_CONT:
  139. case DA9055_REG_LDO2_CONT:
  140. case DA9055_REG_LDO3_CONT:
  141. case DA9055_REG_LDO4_CONT:
  142. case DA9055_REG_LDO5_CONT:
  143. case DA9055_REG_LDO6_CONT:
  144. case DA9055_REG_BUCK_LIM:
  145. case DA9055_REG_BCORE_MODE:
  146. case DA9055_REG_VBCORE_A:
  147. case DA9055_REG_VBMEM_A:
  148. case DA9055_REG_VLDO1_A:
  149. case DA9055_REG_VLDO2_A:
  150. case DA9055_REG_VLDO3_A:
  151. case DA9055_REG_VLDO4_A:
  152. case DA9055_REG_VLDO5_A:
  153. case DA9055_REG_VLDO6_A:
  154. case DA9055_REG_VBCORE_B:
  155. case DA9055_REG_VBMEM_B:
  156. case DA9055_REG_VLDO1_B:
  157. case DA9055_REG_VLDO2_B:
  158. case DA9055_REG_VLDO3_B:
  159. case DA9055_REG_VLDO4_B:
  160. case DA9055_REG_VLDO5_B:
  161. case DA9055_REG_VLDO6_B:
  162. return true;
  163. default:
  164. return false;
  165. }
  166. }
  167. static bool da9055_register_volatile(struct device *dev, unsigned int reg)
  168. {
  169. switch (reg) {
  170. case DA9055_REG_STATUS_A:
  171. case DA9055_REG_STATUS_B:
  172. case DA9055_REG_EVENT_A:
  173. case DA9055_REG_EVENT_B:
  174. case DA9055_REG_EVENT_C:
  175. case DA9055_REG_CONTROL_A:
  176. case DA9055_REG_CONTROL_E:
  177. case DA9055_REG_ADC_MAN:
  178. case DA9055_REG_ADC_RES_L:
  179. case DA9055_REG_ADC_RES_H:
  180. case DA9055_REG_VSYS_RES:
  181. case DA9055_REG_ADCIN1_RES:
  182. case DA9055_REG_ADCIN2_RES:
  183. case DA9055_REG_ADCIN3_RES:
  184. case DA9055_REG_COUNT_S:
  185. case DA9055_REG_COUNT_MI:
  186. case DA9055_REG_COUNT_H:
  187. case DA9055_REG_COUNT_D:
  188. case DA9055_REG_COUNT_MO:
  189. case DA9055_REG_COUNT_Y:
  190. case DA9055_REG_ALARM_MI:
  191. case DA9055_REG_BCORE_CONT:
  192. case DA9055_REG_BMEM_CONT:
  193. case DA9055_REG_LDO1_CONT:
  194. case DA9055_REG_LDO2_CONT:
  195. case DA9055_REG_LDO3_CONT:
  196. case DA9055_REG_LDO4_CONT:
  197. case DA9055_REG_LDO5_CONT:
  198. case DA9055_REG_LDO6_CONT:
  199. return true;
  200. default:
  201. return false;
  202. }
  203. }
  204. static const struct regmap_irq da9055_irqs[] = {
  205. [DA9055_IRQ_NONKEY] = {
  206. .reg_offset = 0,
  207. .mask = DA9055_IRQ_NONKEY_MASK,
  208. },
  209. [DA9055_IRQ_ALARM] = {
  210. .reg_offset = 0,
  211. .mask = DA9055_IRQ_ALM_MASK,
  212. },
  213. [DA9055_IRQ_TICK] = {
  214. .reg_offset = 0,
  215. .mask = DA9055_IRQ_TICK_MASK,
  216. },
  217. [DA9055_IRQ_HWMON] = {
  218. .reg_offset = 0,
  219. .mask = DA9055_IRQ_ADC_MASK,
  220. },
  221. [DA9055_IRQ_REGULATOR] = {
  222. .reg_offset = 1,
  223. .mask = DA9055_IRQ_BUCK_ILIM_MASK,
  224. },
  225. };
  226. const struct regmap_config da9055_regmap_config = {
  227. .reg_bits = 8,
  228. .val_bits = 8,
  229. .cache_type = REGCACHE_RBTREE,
  230. .max_register = DA9055_MAX_REGISTER_CNT,
  231. .readable_reg = da9055_register_readable,
  232. .writeable_reg = da9055_register_writeable,
  233. .volatile_reg = da9055_register_volatile,
  234. };
  235. EXPORT_SYMBOL_GPL(da9055_regmap_config);
  236. static struct resource da9055_onkey_resource = {
  237. .name = "ONKEY",
  238. .start = DA9055_IRQ_NONKEY,
  239. .end = DA9055_IRQ_NONKEY,
  240. .flags = IORESOURCE_IRQ,
  241. };
  242. static struct resource da9055_rtc_resource[] = {
  243. {
  244. .name = "ALM",
  245. .start = DA9055_IRQ_ALARM,
  246. .end = DA9055_IRQ_ALARM,
  247. .flags = IORESOURCE_IRQ,
  248. },
  249. {
  250. .name = "TICK",
  251. .start = DA9055_IRQ_TICK,
  252. .end = DA9055_IRQ_TICK,
  253. .flags = IORESOURCE_IRQ,
  254. },
  255. };
  256. static struct resource da9055_hwmon_resource = {
  257. .name = "HWMON",
  258. .start = DA9055_IRQ_HWMON,
  259. .end = DA9055_IRQ_HWMON,
  260. .flags = IORESOURCE_IRQ,
  261. };
  262. static struct resource da9055_ld05_6_resource = {
  263. .name = "REGULATOR",
  264. .start = DA9055_IRQ_REGULATOR,
  265. .end = DA9055_IRQ_REGULATOR,
  266. .flags = IORESOURCE_IRQ,
  267. };
  268. static const struct mfd_cell da9055_devs[] = {
  269. {
  270. .of_compatible = "dlg,da9055-gpio",
  271. .name = "da9055-gpio",
  272. },
  273. {
  274. .of_compatible = "dlg,da9055-regulator",
  275. .name = "da9055-regulator",
  276. .id = 1,
  277. },
  278. {
  279. .of_compatible = "dlg,da9055-regulator",
  280. .name = "da9055-regulator",
  281. .id = 2,
  282. },
  283. {
  284. .of_compatible = "dlg,da9055-regulator",
  285. .name = "da9055-regulator",
  286. .id = 3,
  287. },
  288. {
  289. .of_compatible = "dlg,da9055-regulator",
  290. .name = "da9055-regulator",
  291. .id = 4,
  292. },
  293. {
  294. .of_compatible = "dlg,da9055-regulator",
  295. .name = "da9055-regulator",
  296. .id = 5,
  297. },
  298. {
  299. .of_compatible = "dlg,da9055-regulator",
  300. .name = "da9055-regulator",
  301. .id = 6,
  302. },
  303. {
  304. .of_compatible = "dlg,da9055-regulator",
  305. .name = "da9055-regulator",
  306. .id = 7,
  307. .resources = &da9055_ld05_6_resource,
  308. .num_resources = 1,
  309. },
  310. {
  311. .of_compatible = "dlg,da9055-regulator",
  312. .name = "da9055-regulator",
  313. .resources = &da9055_ld05_6_resource,
  314. .num_resources = 1,
  315. .id = 8,
  316. },
  317. {
  318. .of_compatible = "dlg,da9055-onkey",
  319. .name = "da9055-onkey",
  320. .resources = &da9055_onkey_resource,
  321. .num_resources = 1,
  322. },
  323. {
  324. .of_compatible = "dlg,da9055-rtc",
  325. .name = "da9055-rtc",
  326. .resources = da9055_rtc_resource,
  327. .num_resources = ARRAY_SIZE(da9055_rtc_resource),
  328. },
  329. {
  330. .of_compatible = "dlg,da9055-hwmon",
  331. .name = "da9055-hwmon",
  332. .resources = &da9055_hwmon_resource,
  333. .num_resources = 1,
  334. },
  335. {
  336. .of_compatible = "dlg,da9055-watchdog",
  337. .name = "da9055-watchdog",
  338. },
  339. };
  340. static const struct regmap_irq_chip da9055_regmap_irq_chip = {
  341. .name = "da9055_irq",
  342. .status_base = DA9055_REG_EVENT_A,
  343. .mask_base = DA9055_REG_IRQ_MASK_A,
  344. .ack_base = DA9055_REG_EVENT_A,
  345. .num_regs = 3,
  346. .irqs = da9055_irqs,
  347. .num_irqs = ARRAY_SIZE(da9055_irqs),
  348. };
  349. int da9055_device_init(struct da9055 *da9055)
  350. {
  351. struct da9055_pdata *pdata = dev_get_platdata(da9055->dev);
  352. int ret;
  353. uint8_t clear_events[3] = {0xFF, 0xFF, 0xFF};
  354. if (pdata && pdata->init != NULL)
  355. pdata->init(da9055);
  356. if (!pdata || !pdata->irq_base)
  357. da9055->irq_base = -1;
  358. else
  359. da9055->irq_base = pdata->irq_base;
  360. ret = da9055_group_write(da9055, DA9055_REG_EVENT_A, 3, clear_events);
  361. if (ret < 0)
  362. return ret;
  363. ret = regmap_add_irq_chip(da9055->regmap, da9055->chip_irq,
  364. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  365. da9055->irq_base, &da9055_regmap_irq_chip,
  366. &da9055->irq_data);
  367. if (ret < 0)
  368. return ret;
  369. da9055->irq_base = regmap_irq_chip_get_base(da9055->irq_data);
  370. ret = mfd_add_devices(da9055->dev, -1,
  371. da9055_devs, ARRAY_SIZE(da9055_devs),
  372. NULL, da9055->irq_base, NULL);
  373. if (ret)
  374. goto err;
  375. return 0;
  376. err:
  377. mfd_remove_devices(da9055->dev);
  378. return ret;
  379. }
  380. void da9055_device_exit(struct da9055 *da9055)
  381. {
  382. regmap_del_irq_chip(da9055->chip_irq, da9055->irq_data);
  383. mfd_remove_devices(da9055->dev);
  384. }
  385. MODULE_DESCRIPTION("Core support for the DA9055 PMIC");
  386. MODULE_LICENSE("GPL");
  387. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");