tps65910.c 13 KB

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