da9062-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. /*
  2. * Core, IRQ and I2C device driver for DA9061 and DA9062 PMICs
  3. * Copyright (C) 2015-2017 Dialog Semiconductor
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/regmap.h>
  21. #include <linux/irq.h>
  22. #include <linux/mfd/core.h>
  23. #include <linux/i2c.h>
  24. #include <linux/mfd/da9062/core.h>
  25. #include <linux/mfd/da9062/registers.h>
  26. #include <linux/regulator/of_regulator.h>
  27. #define DA9062_REG_EVENT_A_OFFSET 0
  28. #define DA9062_REG_EVENT_B_OFFSET 1
  29. #define DA9062_REG_EVENT_C_OFFSET 2
  30. static struct regmap_irq da9061_irqs[] = {
  31. /* EVENT A */
  32. [DA9061_IRQ_ONKEY] = {
  33. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  34. .mask = DA9062AA_M_NONKEY_MASK,
  35. },
  36. [DA9061_IRQ_WDG_WARN] = {
  37. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  38. .mask = DA9062AA_M_WDG_WARN_MASK,
  39. },
  40. [DA9061_IRQ_SEQ_RDY] = {
  41. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  42. .mask = DA9062AA_M_SEQ_RDY_MASK,
  43. },
  44. /* EVENT B */
  45. [DA9061_IRQ_TEMP] = {
  46. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  47. .mask = DA9062AA_M_TEMP_MASK,
  48. },
  49. [DA9061_IRQ_LDO_LIM] = {
  50. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  51. .mask = DA9062AA_M_LDO_LIM_MASK,
  52. },
  53. [DA9061_IRQ_DVC_RDY] = {
  54. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  55. .mask = DA9062AA_M_DVC_RDY_MASK,
  56. },
  57. [DA9061_IRQ_VDD_WARN] = {
  58. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  59. .mask = DA9062AA_M_VDD_WARN_MASK,
  60. },
  61. /* EVENT C */
  62. [DA9061_IRQ_GPI0] = {
  63. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  64. .mask = DA9062AA_M_GPI0_MASK,
  65. },
  66. [DA9061_IRQ_GPI1] = {
  67. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  68. .mask = DA9062AA_M_GPI1_MASK,
  69. },
  70. [DA9061_IRQ_GPI2] = {
  71. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  72. .mask = DA9062AA_M_GPI2_MASK,
  73. },
  74. [DA9061_IRQ_GPI3] = {
  75. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  76. .mask = DA9062AA_M_GPI3_MASK,
  77. },
  78. [DA9061_IRQ_GPI4] = {
  79. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  80. .mask = DA9062AA_M_GPI4_MASK,
  81. },
  82. };
  83. static struct regmap_irq_chip da9061_irq_chip = {
  84. .name = "da9061-irq",
  85. .irqs = da9061_irqs,
  86. .num_irqs = DA9061_NUM_IRQ,
  87. .num_regs = 3,
  88. .status_base = DA9062AA_EVENT_A,
  89. .mask_base = DA9062AA_IRQ_MASK_A,
  90. .ack_base = DA9062AA_EVENT_A,
  91. };
  92. static struct regmap_irq da9062_irqs[] = {
  93. /* EVENT A */
  94. [DA9062_IRQ_ONKEY] = {
  95. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  96. .mask = DA9062AA_M_NONKEY_MASK,
  97. },
  98. [DA9062_IRQ_ALARM] = {
  99. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  100. .mask = DA9062AA_M_ALARM_MASK,
  101. },
  102. [DA9062_IRQ_TICK] = {
  103. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  104. .mask = DA9062AA_M_TICK_MASK,
  105. },
  106. [DA9062_IRQ_WDG_WARN] = {
  107. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  108. .mask = DA9062AA_M_WDG_WARN_MASK,
  109. },
  110. [DA9062_IRQ_SEQ_RDY] = {
  111. .reg_offset = DA9062_REG_EVENT_A_OFFSET,
  112. .mask = DA9062AA_M_SEQ_RDY_MASK,
  113. },
  114. /* EVENT B */
  115. [DA9062_IRQ_TEMP] = {
  116. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  117. .mask = DA9062AA_M_TEMP_MASK,
  118. },
  119. [DA9062_IRQ_LDO_LIM] = {
  120. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  121. .mask = DA9062AA_M_LDO_LIM_MASK,
  122. },
  123. [DA9062_IRQ_DVC_RDY] = {
  124. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  125. .mask = DA9062AA_M_DVC_RDY_MASK,
  126. },
  127. [DA9062_IRQ_VDD_WARN] = {
  128. .reg_offset = DA9062_REG_EVENT_B_OFFSET,
  129. .mask = DA9062AA_M_VDD_WARN_MASK,
  130. },
  131. /* EVENT C */
  132. [DA9062_IRQ_GPI0] = {
  133. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  134. .mask = DA9062AA_M_GPI0_MASK,
  135. },
  136. [DA9062_IRQ_GPI1] = {
  137. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  138. .mask = DA9062AA_M_GPI1_MASK,
  139. },
  140. [DA9062_IRQ_GPI2] = {
  141. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  142. .mask = DA9062AA_M_GPI2_MASK,
  143. },
  144. [DA9062_IRQ_GPI3] = {
  145. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  146. .mask = DA9062AA_M_GPI3_MASK,
  147. },
  148. [DA9062_IRQ_GPI4] = {
  149. .reg_offset = DA9062_REG_EVENT_C_OFFSET,
  150. .mask = DA9062AA_M_GPI4_MASK,
  151. },
  152. };
  153. static struct regmap_irq_chip da9062_irq_chip = {
  154. .name = "da9062-irq",
  155. .irqs = da9062_irqs,
  156. .num_irqs = DA9062_NUM_IRQ,
  157. .num_regs = 3,
  158. .status_base = DA9062AA_EVENT_A,
  159. .mask_base = DA9062AA_IRQ_MASK_A,
  160. .ack_base = DA9062AA_EVENT_A,
  161. };
  162. static struct resource da9061_core_resources[] = {
  163. DEFINE_RES_IRQ_NAMED(DA9061_IRQ_VDD_WARN, "VDD_WARN"),
  164. };
  165. static struct resource da9061_regulators_resources[] = {
  166. DEFINE_RES_IRQ_NAMED(DA9061_IRQ_LDO_LIM, "LDO_LIM"),
  167. };
  168. static struct resource da9061_thermal_resources[] = {
  169. DEFINE_RES_IRQ_NAMED(DA9061_IRQ_TEMP, "THERMAL"),
  170. };
  171. static struct resource da9061_wdt_resources[] = {
  172. DEFINE_RES_IRQ_NAMED(DA9061_IRQ_WDG_WARN, "WD_WARN"),
  173. };
  174. static struct resource da9061_onkey_resources[] = {
  175. DEFINE_RES_IRQ_NAMED(DA9061_IRQ_ONKEY, "ONKEY"),
  176. };
  177. static const struct mfd_cell da9061_devs[] = {
  178. {
  179. .name = "da9061-core",
  180. .num_resources = ARRAY_SIZE(da9061_core_resources),
  181. .resources = da9061_core_resources,
  182. },
  183. {
  184. .name = "da9062-regulators",
  185. .num_resources = ARRAY_SIZE(da9061_regulators_resources),
  186. .resources = da9061_regulators_resources,
  187. },
  188. {
  189. .name = "da9061-watchdog",
  190. .num_resources = ARRAY_SIZE(da9061_wdt_resources),
  191. .resources = da9061_wdt_resources,
  192. .of_compatible = "dlg,da9061-watchdog",
  193. },
  194. {
  195. .name = "da9061-thermal",
  196. .num_resources = ARRAY_SIZE(da9061_thermal_resources),
  197. .resources = da9061_thermal_resources,
  198. .of_compatible = "dlg,da9061-thermal",
  199. },
  200. {
  201. .name = "da9061-onkey",
  202. .num_resources = ARRAY_SIZE(da9061_onkey_resources),
  203. .resources = da9061_onkey_resources,
  204. .of_compatible = "dlg,da9061-onkey",
  205. },
  206. };
  207. static struct resource da9062_core_resources[] = {
  208. DEFINE_RES_NAMED(DA9062_IRQ_VDD_WARN, 1, "VDD_WARN", IORESOURCE_IRQ),
  209. };
  210. static struct resource da9062_regulators_resources[] = {
  211. DEFINE_RES_NAMED(DA9062_IRQ_LDO_LIM, 1, "LDO_LIM", IORESOURCE_IRQ),
  212. };
  213. static struct resource da9062_thermal_resources[] = {
  214. DEFINE_RES_NAMED(DA9062_IRQ_TEMP, 1, "THERMAL", IORESOURCE_IRQ),
  215. };
  216. static struct resource da9062_wdt_resources[] = {
  217. DEFINE_RES_NAMED(DA9062_IRQ_WDG_WARN, 1, "WD_WARN", IORESOURCE_IRQ),
  218. };
  219. static struct resource da9062_rtc_resources[] = {
  220. DEFINE_RES_NAMED(DA9062_IRQ_ALARM, 1, "ALARM", IORESOURCE_IRQ),
  221. DEFINE_RES_NAMED(DA9062_IRQ_TICK, 1, "TICK", IORESOURCE_IRQ),
  222. };
  223. static struct resource da9062_onkey_resources[] = {
  224. DEFINE_RES_NAMED(DA9062_IRQ_ONKEY, 1, "ONKEY", IORESOURCE_IRQ),
  225. };
  226. static const struct mfd_cell da9062_devs[] = {
  227. {
  228. .name = "da9062-core",
  229. .num_resources = ARRAY_SIZE(da9062_core_resources),
  230. .resources = da9062_core_resources,
  231. },
  232. {
  233. .name = "da9062-regulators",
  234. .num_resources = ARRAY_SIZE(da9062_regulators_resources),
  235. .resources = da9062_regulators_resources,
  236. },
  237. {
  238. .name = "da9062-watchdog",
  239. .num_resources = ARRAY_SIZE(da9062_wdt_resources),
  240. .resources = da9062_wdt_resources,
  241. .of_compatible = "dlg,da9062-watchdog",
  242. },
  243. {
  244. .name = "da9062-thermal",
  245. .num_resources = ARRAY_SIZE(da9062_thermal_resources),
  246. .resources = da9062_thermal_resources,
  247. .of_compatible = "dlg,da9062-thermal",
  248. },
  249. {
  250. .name = "da9062-rtc",
  251. .num_resources = ARRAY_SIZE(da9062_rtc_resources),
  252. .resources = da9062_rtc_resources,
  253. .of_compatible = "dlg,da9062-rtc",
  254. },
  255. {
  256. .name = "da9062-onkey",
  257. .num_resources = ARRAY_SIZE(da9062_onkey_resources),
  258. .resources = da9062_onkey_resources,
  259. .of_compatible = "dlg,da9062-onkey",
  260. },
  261. };
  262. static int da9062_clear_fault_log(struct da9062 *chip)
  263. {
  264. int ret;
  265. int fault_log;
  266. ret = regmap_read(chip->regmap, DA9062AA_FAULT_LOG, &fault_log);
  267. if (ret < 0)
  268. return ret;
  269. if (fault_log) {
  270. if (fault_log & DA9062AA_TWD_ERROR_MASK)
  271. dev_dbg(chip->dev, "Fault log entry detected: TWD_ERROR\n");
  272. if (fault_log & DA9062AA_POR_MASK)
  273. dev_dbg(chip->dev, "Fault log entry detected: POR\n");
  274. if (fault_log & DA9062AA_VDD_FAULT_MASK)
  275. dev_dbg(chip->dev, "Fault log entry detected: VDD_FAULT\n");
  276. if (fault_log & DA9062AA_VDD_START_MASK)
  277. dev_dbg(chip->dev, "Fault log entry detected: VDD_START\n");
  278. if (fault_log & DA9062AA_TEMP_CRIT_MASK)
  279. dev_dbg(chip->dev, "Fault log entry detected: TEMP_CRIT\n");
  280. if (fault_log & DA9062AA_KEY_RESET_MASK)
  281. dev_dbg(chip->dev, "Fault log entry detected: KEY_RESET\n");
  282. if (fault_log & DA9062AA_NSHUTDOWN_MASK)
  283. dev_dbg(chip->dev, "Fault log entry detected: NSHUTDOWN\n");
  284. if (fault_log & DA9062AA_WAIT_SHUT_MASK)
  285. dev_dbg(chip->dev, "Fault log entry detected: WAIT_SHUT\n");
  286. ret = regmap_write(chip->regmap, DA9062AA_FAULT_LOG,
  287. fault_log);
  288. }
  289. return ret;
  290. }
  291. static int da9062_get_device_type(struct da9062 *chip)
  292. {
  293. int device_id, variant_id, variant_mrc, variant_vrc;
  294. char *type;
  295. int ret;
  296. ret = regmap_read(chip->regmap, DA9062AA_DEVICE_ID, &device_id);
  297. if (ret < 0) {
  298. dev_err(chip->dev, "Cannot read chip ID.\n");
  299. return -EIO;
  300. }
  301. if (device_id != DA9062_PMIC_DEVICE_ID) {
  302. dev_err(chip->dev, "Invalid device ID: 0x%02x\n", device_id);
  303. return -ENODEV;
  304. }
  305. ret = regmap_read(chip->regmap, DA9062AA_VARIANT_ID, &variant_id);
  306. if (ret < 0) {
  307. dev_err(chip->dev, "Cannot read chip variant id.\n");
  308. return -EIO;
  309. }
  310. variant_vrc = (variant_id & DA9062AA_VRC_MASK) >> DA9062AA_VRC_SHIFT;
  311. switch (variant_vrc) {
  312. case DA9062_PMIC_VARIANT_VRC_DA9061:
  313. type = "DA9061";
  314. break;
  315. case DA9062_PMIC_VARIANT_VRC_DA9062:
  316. type = "DA9062";
  317. break;
  318. default:
  319. type = "Unknown";
  320. break;
  321. }
  322. dev_info(chip->dev,
  323. "Device detected (device-ID: 0x%02X, var-ID: 0x%02X, %s)\n",
  324. device_id, variant_id, type);
  325. variant_mrc = (variant_id & DA9062AA_MRC_MASK) >> DA9062AA_MRC_SHIFT;
  326. if (variant_mrc < DA9062_PMIC_VARIANT_MRC_AA) {
  327. dev_err(chip->dev,
  328. "Cannot support variant MRC: 0x%02X\n", variant_mrc);
  329. return -ENODEV;
  330. }
  331. return ret;
  332. }
  333. static const struct regmap_range da9061_aa_readable_ranges[] = {
  334. regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
  335. regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
  336. regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
  337. regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
  338. regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_GPIO_OUT3_4),
  339. regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
  340. regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
  341. regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
  342. regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
  343. regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
  344. regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
  345. regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
  346. regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_WAIT),
  347. regmap_reg_range(DA9062AA_RESET, DA9062AA_BUCK_ILIM_C),
  348. regmap_reg_range(DA9062AA_BUCK1_CFG, DA9062AA_BUCK3_CFG),
  349. regmap_reg_range(DA9062AA_VBUCK1_A, DA9062AA_VBUCK4_A),
  350. regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
  351. regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
  352. regmap_reg_range(DA9062AA_VBUCK1_B, DA9062AA_VBUCK4_B),
  353. regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
  354. regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
  355. regmap_reg_range(DA9062AA_INTERFACE, DA9062AA_CONFIG_E),
  356. regmap_reg_range(DA9062AA_CONFIG_G, DA9062AA_CONFIG_K),
  357. regmap_reg_range(DA9062AA_CONFIG_M, DA9062AA_CONFIG_M),
  358. regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
  359. regmap_reg_range(DA9062AA_DEVICE_ID, DA9062AA_CONFIG_ID),
  360. };
  361. static const struct regmap_range da9061_aa_writeable_ranges[] = {
  362. regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_PAGE_CON),
  363. regmap_reg_range(DA9062AA_FAULT_LOG, DA9062AA_EVENT_C),
  364. regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
  365. regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
  366. regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_GPIO_OUT3_4),
  367. regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
  368. regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
  369. regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
  370. regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
  371. regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
  372. regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
  373. regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
  374. regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_WAIT),
  375. regmap_reg_range(DA9062AA_RESET, DA9062AA_BUCK_ILIM_C),
  376. regmap_reg_range(DA9062AA_BUCK1_CFG, DA9062AA_BUCK3_CFG),
  377. regmap_reg_range(DA9062AA_VBUCK1_A, DA9062AA_VBUCK4_A),
  378. regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
  379. regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
  380. regmap_reg_range(DA9062AA_VBUCK1_B, DA9062AA_VBUCK4_B),
  381. regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
  382. regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
  383. regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
  384. };
  385. static const struct regmap_range da9061_aa_volatile_ranges[] = {
  386. regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
  387. regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
  388. regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_CONTROL_B),
  389. regmap_reg_range(DA9062AA_CONTROL_E, DA9062AA_CONTROL_F),
  390. regmap_reg_range(DA9062AA_BUCK1_CONT, DA9062AA_BUCK4_CONT),
  391. regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
  392. regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
  393. regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
  394. regmap_reg_range(DA9062AA_SEQ, DA9062AA_SEQ),
  395. };
  396. static const struct regmap_access_table da9061_aa_readable_table = {
  397. .yes_ranges = da9061_aa_readable_ranges,
  398. .n_yes_ranges = ARRAY_SIZE(da9061_aa_readable_ranges),
  399. };
  400. static const struct regmap_access_table da9061_aa_writeable_table = {
  401. .yes_ranges = da9061_aa_writeable_ranges,
  402. .n_yes_ranges = ARRAY_SIZE(da9061_aa_writeable_ranges),
  403. };
  404. static const struct regmap_access_table da9061_aa_volatile_table = {
  405. .yes_ranges = da9061_aa_volatile_ranges,
  406. .n_yes_ranges = ARRAY_SIZE(da9061_aa_volatile_ranges),
  407. };
  408. static const struct regmap_range_cfg da9061_range_cfg[] = {
  409. {
  410. .range_min = DA9062AA_PAGE_CON,
  411. .range_max = DA9062AA_CONFIG_ID,
  412. .selector_reg = DA9062AA_PAGE_CON,
  413. .selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
  414. .selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
  415. .window_start = 0,
  416. .window_len = 256,
  417. }
  418. };
  419. static struct regmap_config da9061_regmap_config = {
  420. .reg_bits = 8,
  421. .val_bits = 8,
  422. .ranges = da9061_range_cfg,
  423. .num_ranges = ARRAY_SIZE(da9061_range_cfg),
  424. .max_register = DA9062AA_CONFIG_ID,
  425. .cache_type = REGCACHE_RBTREE,
  426. .rd_table = &da9061_aa_readable_table,
  427. .wr_table = &da9061_aa_writeable_table,
  428. .volatile_table = &da9061_aa_volatile_table,
  429. };
  430. static const struct regmap_range da9062_aa_readable_ranges[] = {
  431. regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
  432. regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
  433. regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
  434. regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
  435. regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_BUCK4_CONT),
  436. regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
  437. regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
  438. regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
  439. regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_SECOND_D),
  440. regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
  441. regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
  442. regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
  443. regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_BUCK3_CFG),
  444. regmap_reg_range(DA9062AA_VBUCK2_A, DA9062AA_VBUCK4_A),
  445. regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
  446. regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
  447. regmap_reg_range(DA9062AA_VBUCK2_B, DA9062AA_VBUCK4_B),
  448. regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
  449. regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
  450. regmap_reg_range(DA9062AA_BBAT_CONT, DA9062AA_BBAT_CONT),
  451. regmap_reg_range(DA9062AA_INTERFACE, DA9062AA_CONFIG_E),
  452. regmap_reg_range(DA9062AA_CONFIG_G, DA9062AA_CONFIG_K),
  453. regmap_reg_range(DA9062AA_CONFIG_M, DA9062AA_CONFIG_M),
  454. regmap_reg_range(DA9062AA_TRIM_CLDR, DA9062AA_GP_ID_19),
  455. regmap_reg_range(DA9062AA_DEVICE_ID, DA9062AA_CONFIG_ID),
  456. };
  457. static const struct regmap_range da9062_aa_writeable_ranges[] = {
  458. regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_PAGE_CON),
  459. regmap_reg_range(DA9062AA_FAULT_LOG, DA9062AA_EVENT_C),
  460. regmap_reg_range(DA9062AA_IRQ_MASK_A, DA9062AA_IRQ_MASK_C),
  461. regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_GPIO_4),
  462. regmap_reg_range(DA9062AA_GPIO_WKUP_MODE, DA9062AA_BUCK4_CONT),
  463. regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
  464. regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
  465. regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
  466. regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_ALARM_Y),
  467. regmap_reg_range(DA9062AA_SEQ, DA9062AA_ID_4_3),
  468. regmap_reg_range(DA9062AA_ID_12_11, DA9062AA_ID_16_15),
  469. regmap_reg_range(DA9062AA_ID_22_21, DA9062AA_ID_32_31),
  470. regmap_reg_range(DA9062AA_SEQ_A, DA9062AA_BUCK3_CFG),
  471. regmap_reg_range(DA9062AA_VBUCK2_A, DA9062AA_VBUCK4_A),
  472. regmap_reg_range(DA9062AA_VBUCK3_A, DA9062AA_VBUCK3_A),
  473. regmap_reg_range(DA9062AA_VLDO1_A, DA9062AA_VLDO4_A),
  474. regmap_reg_range(DA9062AA_VBUCK2_B, DA9062AA_VBUCK4_B),
  475. regmap_reg_range(DA9062AA_VBUCK3_B, DA9062AA_VBUCK3_B),
  476. regmap_reg_range(DA9062AA_VLDO1_B, DA9062AA_VLDO4_B),
  477. regmap_reg_range(DA9062AA_BBAT_CONT, DA9062AA_BBAT_CONT),
  478. regmap_reg_range(DA9062AA_GP_ID_0, DA9062AA_GP_ID_19),
  479. };
  480. static const struct regmap_range da9062_aa_volatile_ranges[] = {
  481. regmap_reg_range(DA9062AA_PAGE_CON, DA9062AA_STATUS_B),
  482. regmap_reg_range(DA9062AA_STATUS_D, DA9062AA_EVENT_C),
  483. regmap_reg_range(DA9062AA_CONTROL_A, DA9062AA_CONTROL_B),
  484. regmap_reg_range(DA9062AA_CONTROL_E, DA9062AA_CONTROL_F),
  485. regmap_reg_range(DA9062AA_BUCK2_CONT, DA9062AA_BUCK4_CONT),
  486. regmap_reg_range(DA9062AA_BUCK3_CONT, DA9062AA_BUCK3_CONT),
  487. regmap_reg_range(DA9062AA_LDO1_CONT, DA9062AA_LDO4_CONT),
  488. regmap_reg_range(DA9062AA_DVC_1, DA9062AA_DVC_1),
  489. regmap_reg_range(DA9062AA_COUNT_S, DA9062AA_SECOND_D),
  490. regmap_reg_range(DA9062AA_SEQ, DA9062AA_SEQ),
  491. regmap_reg_range(DA9062AA_EN_32K, DA9062AA_EN_32K),
  492. };
  493. static const struct regmap_access_table da9062_aa_readable_table = {
  494. .yes_ranges = da9062_aa_readable_ranges,
  495. .n_yes_ranges = ARRAY_SIZE(da9062_aa_readable_ranges),
  496. };
  497. static const struct regmap_access_table da9062_aa_writeable_table = {
  498. .yes_ranges = da9062_aa_writeable_ranges,
  499. .n_yes_ranges = ARRAY_SIZE(da9062_aa_writeable_ranges),
  500. };
  501. static const struct regmap_access_table da9062_aa_volatile_table = {
  502. .yes_ranges = da9062_aa_volatile_ranges,
  503. .n_yes_ranges = ARRAY_SIZE(da9062_aa_volatile_ranges),
  504. };
  505. static const struct regmap_range_cfg da9062_range_cfg[] = {
  506. {
  507. .range_min = DA9062AA_PAGE_CON,
  508. .range_max = DA9062AA_CONFIG_ID,
  509. .selector_reg = DA9062AA_PAGE_CON,
  510. .selector_mask = 1 << DA9062_I2C_PAGE_SEL_SHIFT,
  511. .selector_shift = DA9062_I2C_PAGE_SEL_SHIFT,
  512. .window_start = 0,
  513. .window_len = 256,
  514. }
  515. };
  516. static struct regmap_config da9062_regmap_config = {
  517. .reg_bits = 8,
  518. .val_bits = 8,
  519. .ranges = da9062_range_cfg,
  520. .num_ranges = ARRAY_SIZE(da9062_range_cfg),
  521. .max_register = DA9062AA_CONFIG_ID,
  522. .cache_type = REGCACHE_RBTREE,
  523. .rd_table = &da9062_aa_readable_table,
  524. .wr_table = &da9062_aa_writeable_table,
  525. .volatile_table = &da9062_aa_volatile_table,
  526. };
  527. static const struct of_device_id da9062_dt_ids[] = {
  528. { .compatible = "dlg,da9061", .data = (void *)COMPAT_TYPE_DA9061, },
  529. { .compatible = "dlg,da9062", .data = (void *)COMPAT_TYPE_DA9062, },
  530. { }
  531. };
  532. MODULE_DEVICE_TABLE(of, da9062_dt_ids);
  533. static int da9062_i2c_probe(struct i2c_client *i2c,
  534. const struct i2c_device_id *id)
  535. {
  536. struct da9062 *chip;
  537. const struct of_device_id *match;
  538. unsigned int irq_base;
  539. const struct mfd_cell *cell;
  540. const struct regmap_irq_chip *irq_chip;
  541. const struct regmap_config *config;
  542. int cell_num;
  543. int ret;
  544. chip = devm_kzalloc(&i2c->dev, sizeof(*chip), GFP_KERNEL);
  545. if (!chip)
  546. return -ENOMEM;
  547. if (i2c->dev.of_node) {
  548. match = of_match_node(da9062_dt_ids, i2c->dev.of_node);
  549. if (!match)
  550. return -EINVAL;
  551. chip->chip_type = (uintptr_t)match->data;
  552. } else {
  553. chip->chip_type = id->driver_data;
  554. }
  555. i2c_set_clientdata(i2c, chip);
  556. chip->dev = &i2c->dev;
  557. if (!i2c->irq) {
  558. dev_err(chip->dev, "No IRQ configured\n");
  559. return -EINVAL;
  560. }
  561. switch (chip->chip_type) {
  562. case COMPAT_TYPE_DA9061:
  563. cell = da9061_devs;
  564. cell_num = ARRAY_SIZE(da9061_devs);
  565. irq_chip = &da9061_irq_chip;
  566. config = &da9061_regmap_config;
  567. break;
  568. case COMPAT_TYPE_DA9062:
  569. cell = da9062_devs;
  570. cell_num = ARRAY_SIZE(da9062_devs);
  571. irq_chip = &da9062_irq_chip;
  572. config = &da9062_regmap_config;
  573. break;
  574. default:
  575. dev_err(chip->dev, "Unrecognised chip type\n");
  576. return -ENODEV;
  577. }
  578. chip->regmap = devm_regmap_init_i2c(i2c, config);
  579. if (IS_ERR(chip->regmap)) {
  580. ret = PTR_ERR(chip->regmap);
  581. dev_err(chip->dev, "Failed to allocate register map: %d\n",
  582. ret);
  583. return ret;
  584. }
  585. ret = da9062_clear_fault_log(chip);
  586. if (ret < 0)
  587. dev_warn(chip->dev, "Cannot clear fault log\n");
  588. ret = da9062_get_device_type(chip);
  589. if (ret)
  590. return ret;
  591. ret = regmap_add_irq_chip(chip->regmap, i2c->irq,
  592. IRQF_TRIGGER_LOW | IRQF_ONESHOT | IRQF_SHARED,
  593. -1, irq_chip,
  594. &chip->regmap_irq);
  595. if (ret) {
  596. dev_err(chip->dev, "Failed to request IRQ %d: %d\n",
  597. i2c->irq, ret);
  598. return ret;
  599. }
  600. irq_base = regmap_irq_chip_get_base(chip->regmap_irq);
  601. ret = mfd_add_devices(chip->dev, PLATFORM_DEVID_NONE, cell,
  602. cell_num, NULL, irq_base,
  603. NULL);
  604. if (ret) {
  605. dev_err(chip->dev, "Cannot register child devices\n");
  606. regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
  607. return ret;
  608. }
  609. return ret;
  610. }
  611. static int da9062_i2c_remove(struct i2c_client *i2c)
  612. {
  613. struct da9062 *chip = i2c_get_clientdata(i2c);
  614. mfd_remove_devices(chip->dev);
  615. regmap_del_irq_chip(i2c->irq, chip->regmap_irq);
  616. return 0;
  617. }
  618. static const struct i2c_device_id da9062_i2c_id[] = {
  619. { "da9061", COMPAT_TYPE_DA9061 },
  620. { "da9062", COMPAT_TYPE_DA9062 },
  621. { },
  622. };
  623. MODULE_DEVICE_TABLE(i2c, da9062_i2c_id);
  624. static struct i2c_driver da9062_i2c_driver = {
  625. .driver = {
  626. .name = "da9062",
  627. .of_match_table = of_match_ptr(da9062_dt_ids),
  628. },
  629. .probe = da9062_i2c_probe,
  630. .remove = da9062_i2c_remove,
  631. .id_table = da9062_i2c_id,
  632. };
  633. module_i2c_driver(da9062_i2c_driver);
  634. MODULE_DESCRIPTION("Core device driver for Dialog DA9061 and DA9062");
  635. MODULE_AUTHOR("Steve Twiss <stwiss.opensource@diasemi.com>");
  636. MODULE_LICENSE("GPL");