tps65910.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tps65910.c -- TI TPS6591x chip family multi-function driver
  4. *
  5. * Copyright 2010 Texas Instruments Inc.
  6. *
  7. * Author: Graeme Gregory <gg@slimlogic.co.uk>
  8. * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/err.h>
  12. #include <linux/slab.h>
  13. #include <linux/i2c.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/mfd/core.h>
  18. #include <linux/regmap.h>
  19. #include <linux/mfd/tps65910.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. static struct resource rtc_resources[] = {
  23. {
  24. .start = TPS65910_IRQ_RTC_ALARM,
  25. .end = TPS65910_IRQ_RTC_ALARM,
  26. .flags = IORESOURCE_IRQ,
  27. }
  28. };
  29. static const struct mfd_cell tps65910s[] = {
  30. {
  31. .name = "tps65910-gpio",
  32. },
  33. {
  34. .name = "tps65910-pmic",
  35. },
  36. {
  37. .name = "tps65910-rtc",
  38. .num_resources = ARRAY_SIZE(rtc_resources),
  39. .resources = &rtc_resources[0],
  40. },
  41. {
  42. .name = "tps65910-power",
  43. },
  44. };
  45. static const struct regmap_irq tps65911_irqs[] = {
  46. /* INT_STS */
  47. [TPS65911_IRQ_PWRHOLD_F] = {
  48. .mask = INT_MSK_PWRHOLD_F_IT_MSK_MASK,
  49. .reg_offset = 0,
  50. },
  51. [TPS65911_IRQ_VBAT_VMHI] = {
  52. .mask = INT_MSK_VMBHI_IT_MSK_MASK,
  53. .reg_offset = 0,
  54. },
  55. [TPS65911_IRQ_PWRON] = {
  56. .mask = INT_MSK_PWRON_IT_MSK_MASK,
  57. .reg_offset = 0,
  58. },
  59. [TPS65911_IRQ_PWRON_LP] = {
  60. .mask = INT_MSK_PWRON_LP_IT_MSK_MASK,
  61. .reg_offset = 0,
  62. },
  63. [TPS65911_IRQ_PWRHOLD_R] = {
  64. .mask = INT_MSK_PWRHOLD_R_IT_MSK_MASK,
  65. .reg_offset = 0,
  66. },
  67. [TPS65911_IRQ_HOTDIE] = {
  68. .mask = INT_MSK_HOTDIE_IT_MSK_MASK,
  69. .reg_offset = 0,
  70. },
  71. [TPS65911_IRQ_RTC_ALARM] = {
  72. .mask = INT_MSK_RTC_ALARM_IT_MSK_MASK,
  73. .reg_offset = 0,
  74. },
  75. [TPS65911_IRQ_RTC_PERIOD] = {
  76. .mask = INT_MSK_RTC_PERIOD_IT_MSK_MASK,
  77. .reg_offset = 0,
  78. },
  79. /* INT_STS2 */
  80. [TPS65911_IRQ_GPIO0_R] = {
  81. .mask = INT_MSK2_GPIO0_R_IT_MSK_MASK,
  82. .reg_offset = 1,
  83. },
  84. [TPS65911_IRQ_GPIO0_F] = {
  85. .mask = INT_MSK2_GPIO0_F_IT_MSK_MASK,
  86. .reg_offset = 1,
  87. },
  88. [TPS65911_IRQ_GPIO1_R] = {
  89. .mask = INT_MSK2_GPIO1_R_IT_MSK_MASK,
  90. .reg_offset = 1,
  91. },
  92. [TPS65911_IRQ_GPIO1_F] = {
  93. .mask = INT_MSK2_GPIO1_F_IT_MSK_MASK,
  94. .reg_offset = 1,
  95. },
  96. [TPS65911_IRQ_GPIO2_R] = {
  97. .mask = INT_MSK2_GPIO2_R_IT_MSK_MASK,
  98. .reg_offset = 1,
  99. },
  100. [TPS65911_IRQ_GPIO2_F] = {
  101. .mask = INT_MSK2_GPIO2_F_IT_MSK_MASK,
  102. .reg_offset = 1,
  103. },
  104. [TPS65911_IRQ_GPIO3_R] = {
  105. .mask = INT_MSK2_GPIO3_R_IT_MSK_MASK,
  106. .reg_offset = 1,
  107. },
  108. [TPS65911_IRQ_GPIO3_F] = {
  109. .mask = INT_MSK2_GPIO3_F_IT_MSK_MASK,
  110. .reg_offset = 1,
  111. },
  112. /* INT_STS2 */
  113. [TPS65911_IRQ_GPIO4_R] = {
  114. .mask = INT_MSK3_GPIO4_R_IT_MSK_MASK,
  115. .reg_offset = 2,
  116. },
  117. [TPS65911_IRQ_GPIO4_F] = {
  118. .mask = INT_MSK3_GPIO4_F_IT_MSK_MASK,
  119. .reg_offset = 2,
  120. },
  121. [TPS65911_IRQ_GPIO5_R] = {
  122. .mask = INT_MSK3_GPIO5_R_IT_MSK_MASK,
  123. .reg_offset = 2,
  124. },
  125. [TPS65911_IRQ_GPIO5_F] = {
  126. .mask = INT_MSK3_GPIO5_F_IT_MSK_MASK,
  127. .reg_offset = 2,
  128. },
  129. [TPS65911_IRQ_WTCHDG] = {
  130. .mask = INT_MSK3_WTCHDG_IT_MSK_MASK,
  131. .reg_offset = 2,
  132. },
  133. [TPS65911_IRQ_VMBCH2_H] = {
  134. .mask = INT_MSK3_VMBCH2_H_IT_MSK_MASK,
  135. .reg_offset = 2,
  136. },
  137. [TPS65911_IRQ_VMBCH2_L] = {
  138. .mask = INT_MSK3_VMBCH2_L_IT_MSK_MASK,
  139. .reg_offset = 2,
  140. },
  141. [TPS65911_IRQ_PWRDN] = {
  142. .mask = INT_MSK3_PWRDN_IT_MSK_MASK,
  143. .reg_offset = 2,
  144. },
  145. };
  146. static const struct regmap_irq tps65910_irqs[] = {
  147. /* INT_STS */
  148. [TPS65910_IRQ_VBAT_VMBDCH] = {
  149. .mask = TPS65910_INT_MSK_VMBDCH_IT_MSK_MASK,
  150. .reg_offset = 0,
  151. },
  152. [TPS65910_IRQ_VBAT_VMHI] = {
  153. .mask = TPS65910_INT_MSK_VMBHI_IT_MSK_MASK,
  154. .reg_offset = 0,
  155. },
  156. [TPS65910_IRQ_PWRON] = {
  157. .mask = TPS65910_INT_MSK_PWRON_IT_MSK_MASK,
  158. .reg_offset = 0,
  159. },
  160. [TPS65910_IRQ_PWRON_LP] = {
  161. .mask = TPS65910_INT_MSK_PWRON_LP_IT_MSK_MASK,
  162. .reg_offset = 0,
  163. },
  164. [TPS65910_IRQ_PWRHOLD] = {
  165. .mask = TPS65910_INT_MSK_PWRHOLD_IT_MSK_MASK,
  166. .reg_offset = 0,
  167. },
  168. [TPS65910_IRQ_HOTDIE] = {
  169. .mask = TPS65910_INT_MSK_HOTDIE_IT_MSK_MASK,
  170. .reg_offset = 0,
  171. },
  172. [TPS65910_IRQ_RTC_ALARM] = {
  173. .mask = TPS65910_INT_MSK_RTC_ALARM_IT_MSK_MASK,
  174. .reg_offset = 0,
  175. },
  176. [TPS65910_IRQ_RTC_PERIOD] = {
  177. .mask = TPS65910_INT_MSK_RTC_PERIOD_IT_MSK_MASK,
  178. .reg_offset = 0,
  179. },
  180. /* INT_STS2 */
  181. [TPS65910_IRQ_GPIO_R] = {
  182. .mask = TPS65910_INT_MSK2_GPIO0_F_IT_MSK_MASK,
  183. .reg_offset = 1,
  184. },
  185. [TPS65910_IRQ_GPIO_F] = {
  186. .mask = TPS65910_INT_MSK2_GPIO0_R_IT_MSK_MASK,
  187. .reg_offset = 1,
  188. },
  189. };
  190. static struct regmap_irq_chip tps65911_irq_chip = {
  191. .name = "tps65910",
  192. .irqs = tps65911_irqs,
  193. .num_irqs = ARRAY_SIZE(tps65911_irqs),
  194. .num_regs = 3,
  195. .irq_reg_stride = 2,
  196. .status_base = TPS65910_INT_STS,
  197. .mask_base = TPS65910_INT_MSK,
  198. .ack_base = TPS65910_INT_STS,
  199. };
  200. static struct regmap_irq_chip tps65910_irq_chip = {
  201. .name = "tps65910",
  202. .irqs = tps65910_irqs,
  203. .num_irqs = ARRAY_SIZE(tps65910_irqs),
  204. .num_regs = 2,
  205. .irq_reg_stride = 2,
  206. .status_base = TPS65910_INT_STS,
  207. .mask_base = TPS65910_INT_MSK,
  208. .ack_base = TPS65910_INT_STS,
  209. };
  210. static int tps65910_irq_init(struct tps65910 *tps65910, int irq,
  211. struct tps65910_platform_data *pdata)
  212. {
  213. int ret;
  214. static struct regmap_irq_chip *tps6591x_irqs_chip;
  215. if (!irq) {
  216. dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n");
  217. return -EINVAL;
  218. }
  219. if (!pdata) {
  220. dev_warn(tps65910->dev, "No interrupt support, no pdata\n");
  221. return -EINVAL;
  222. }
  223. switch (tps65910_chip_id(tps65910)) {
  224. case TPS65910:
  225. tps6591x_irqs_chip = &tps65910_irq_chip;
  226. break;
  227. case TPS65911:
  228. tps6591x_irqs_chip = &tps65911_irq_chip;
  229. break;
  230. }
  231. tps65910->chip_irq = irq;
  232. ret = devm_regmap_add_irq_chip(tps65910->dev, tps65910->regmap,
  233. tps65910->chip_irq,
  234. IRQF_ONESHOT, pdata->irq_base,
  235. tps6591x_irqs_chip, &tps65910->irq_data);
  236. if (ret < 0) {
  237. dev_warn(tps65910->dev, "Failed to add irq_chip %d\n", ret);
  238. tps65910->chip_irq = 0;
  239. }
  240. return ret;
  241. }
  242. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  243. {
  244. struct tps65910 *tps65910 = dev_get_drvdata(dev);
  245. /*
  246. * Caching all regulator registers.
  247. * All regualator register address range is same for
  248. * TPS65910 and TPS65911
  249. */
  250. if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
  251. /* Check for non-existing register */
  252. if (tps65910_chip_id(tps65910) == TPS65910)
  253. if ((reg == TPS65911_VDDCTRL_OP) ||
  254. (reg == TPS65911_VDDCTRL_SR))
  255. return true;
  256. return false;
  257. }
  258. return true;
  259. }
  260. static const struct regmap_config tps65910_regmap_config = {
  261. .reg_bits = 8,
  262. .val_bits = 8,
  263. .volatile_reg = is_volatile_reg,
  264. .max_register = TPS65910_MAX_REGISTER - 1,
  265. .cache_type = REGCACHE_RBTREE,
  266. };
  267. static int tps65910_ck32k_init(struct tps65910 *tps65910,
  268. struct tps65910_board *pmic_pdata)
  269. {
  270. int ret;
  271. if (!pmic_pdata->en_ck32k_xtal)
  272. return 0;
  273. ret = tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  274. DEVCTRL_CK32K_CTRL_MASK);
  275. if (ret < 0) {
  276. dev_err(tps65910->dev, "clear ck32k_ctrl failed: %d\n", ret);
  277. return ret;
  278. }
  279. return 0;
  280. }
  281. static int tps65910_sleepinit(struct tps65910 *tps65910,
  282. struct tps65910_board *pmic_pdata)
  283. {
  284. struct device *dev;
  285. int ret;
  286. if (!pmic_pdata->en_dev_slp)
  287. return 0;
  288. dev = tps65910->dev;
  289. /* enabling SLEEP device state */
  290. ret = tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  291. DEVCTRL_DEV_SLP_MASK);
  292. if (ret < 0) {
  293. dev_err(dev, "set dev_slp failed: %d\n", ret);
  294. goto err_sleep_init;
  295. }
  296. if (pmic_pdata->slp_keepon.therm_keepon) {
  297. ret = tps65910_reg_set_bits(tps65910,
  298. TPS65910_SLEEP_KEEP_RES_ON,
  299. SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
  300. if (ret < 0) {
  301. dev_err(dev, "set therm_keepon failed: %d\n", ret);
  302. goto disable_dev_slp;
  303. }
  304. }
  305. if (pmic_pdata->slp_keepon.clkout32k_keepon) {
  306. ret = tps65910_reg_set_bits(tps65910,
  307. TPS65910_SLEEP_KEEP_RES_ON,
  308. SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
  309. if (ret < 0) {
  310. dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
  311. goto disable_dev_slp;
  312. }
  313. }
  314. if (pmic_pdata->slp_keepon.i2chs_keepon) {
  315. ret = tps65910_reg_set_bits(tps65910,
  316. TPS65910_SLEEP_KEEP_RES_ON,
  317. SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
  318. if (ret < 0) {
  319. dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
  320. goto disable_dev_slp;
  321. }
  322. }
  323. return 0;
  324. disable_dev_slp:
  325. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  326. DEVCTRL_DEV_SLP_MASK);
  327. err_sleep_init:
  328. return ret;
  329. }
  330. #ifdef CONFIG_OF
  331. static const struct of_device_id tps65910_of_match[] = {
  332. { .compatible = "ti,tps65910", .data = (void *)TPS65910},
  333. { .compatible = "ti,tps65911", .data = (void *)TPS65911},
  334. { },
  335. };
  336. static struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  337. unsigned long *chip_id)
  338. {
  339. struct device_node *np = client->dev.of_node;
  340. struct tps65910_board *board_info;
  341. unsigned int prop;
  342. const struct of_device_id *match;
  343. int ret;
  344. match = of_match_device(tps65910_of_match, &client->dev);
  345. if (!match) {
  346. dev_err(&client->dev, "Failed to find matching dt id\n");
  347. return NULL;
  348. }
  349. *chip_id = (unsigned long)match->data;
  350. board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
  351. GFP_KERNEL);
  352. if (!board_info)
  353. return NULL;
  354. ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
  355. if (!ret)
  356. board_info->vmbch_threshold = prop;
  357. ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
  358. if (!ret)
  359. board_info->vmbch2_threshold = prop;
  360. prop = of_property_read_bool(np, "ti,en-ck32k-xtal");
  361. board_info->en_ck32k_xtal = prop;
  362. prop = of_property_read_bool(np, "ti,sleep-enable");
  363. board_info->en_dev_slp = prop;
  364. prop = of_property_read_bool(np, "ti,sleep-keep-therm");
  365. board_info->slp_keepon.therm_keepon = prop;
  366. prop = of_property_read_bool(np, "ti,sleep-keep-ck32k");
  367. board_info->slp_keepon.clkout32k_keepon = prop;
  368. prop = of_property_read_bool(np, "ti,sleep-keep-hsclk");
  369. board_info->slp_keepon.i2chs_keepon = prop;
  370. board_info->irq = client->irq;
  371. board_info->irq_base = -1;
  372. board_info->pm_off = of_property_read_bool(np,
  373. "ti,system-power-controller");
  374. return board_info;
  375. }
  376. #else
  377. static inline
  378. struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  379. unsigned long *chip_id)
  380. {
  381. return NULL;
  382. }
  383. #endif
  384. static struct i2c_client *tps65910_i2c_client;
  385. static void tps65910_power_off(void)
  386. {
  387. struct tps65910 *tps65910;
  388. tps65910 = dev_get_drvdata(&tps65910_i2c_client->dev);
  389. if (tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  390. DEVCTRL_PWR_OFF_MASK) < 0)
  391. return;
  392. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  393. DEVCTRL_DEV_ON_MASK);
  394. }
  395. static int tps65910_i2c_probe(struct i2c_client *i2c,
  396. const struct i2c_device_id *id)
  397. {
  398. struct tps65910 *tps65910;
  399. struct tps65910_board *pmic_plat_data;
  400. struct tps65910_board *of_pmic_plat_data = NULL;
  401. struct tps65910_platform_data *init_data;
  402. unsigned long chip_id = id->driver_data;
  403. int ret;
  404. pmic_plat_data = dev_get_platdata(&i2c->dev);
  405. if (!pmic_plat_data && i2c->dev.of_node) {
  406. pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
  407. of_pmic_plat_data = pmic_plat_data;
  408. }
  409. if (!pmic_plat_data)
  410. return -EINVAL;
  411. init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
  412. if (init_data == NULL)
  413. return -ENOMEM;
  414. tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
  415. if (tps65910 == NULL)
  416. return -ENOMEM;
  417. tps65910->of_plat_data = of_pmic_plat_data;
  418. i2c_set_clientdata(i2c, tps65910);
  419. tps65910->dev = &i2c->dev;
  420. tps65910->i2c_client = i2c;
  421. tps65910->id = chip_id;
  422. /* Work around silicon erratum SWCZ010: the tps65910 may miss the
  423. * first I2C transfer. So issue a dummy transfer before the first
  424. * real transfer.
  425. */
  426. i2c_master_send(i2c, "", 1);
  427. tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
  428. if (IS_ERR(tps65910->regmap)) {
  429. ret = PTR_ERR(tps65910->regmap);
  430. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  431. return ret;
  432. }
  433. init_data->irq = pmic_plat_data->irq;
  434. init_data->irq_base = pmic_plat_data->irq_base;
  435. tps65910_irq_init(tps65910, init_data->irq, init_data);
  436. tps65910_ck32k_init(tps65910, pmic_plat_data);
  437. tps65910_sleepinit(tps65910, pmic_plat_data);
  438. if (pmic_plat_data->pm_off && !pm_power_off) {
  439. tps65910_i2c_client = i2c;
  440. pm_power_off = tps65910_power_off;
  441. }
  442. ret = devm_mfd_add_devices(tps65910->dev, -1,
  443. tps65910s, ARRAY_SIZE(tps65910s),
  444. NULL, 0,
  445. regmap_irq_get_domain(tps65910->irq_data));
  446. if (ret < 0) {
  447. dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
  448. return ret;
  449. }
  450. return ret;
  451. }
  452. static const struct i2c_device_id tps65910_i2c_id[] = {
  453. { "tps65910", TPS65910 },
  454. { "tps65911", TPS65911 },
  455. { }
  456. };
  457. static struct i2c_driver tps65910_i2c_driver = {
  458. .driver = {
  459. .name = "tps65910",
  460. .of_match_table = of_match_ptr(tps65910_of_match),
  461. },
  462. .probe = tps65910_i2c_probe,
  463. .id_table = tps65910_i2c_id,
  464. };
  465. static int __init tps65910_i2c_init(void)
  466. {
  467. return i2c_add_driver(&tps65910_i2c_driver);
  468. }
  469. /* init early so consumer devices can complete system boot */
  470. subsys_initcall(tps65910_i2c_init);