tps65910.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 const 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. tps65910->chip_irq = 0;
  245. }
  246. return ret;
  247. }
  248. static int tps65910_irq_exit(struct tps65910 *tps65910)
  249. {
  250. if (tps65910->chip_irq > 0)
  251. regmap_del_irq_chip(tps65910->chip_irq, tps65910->irq_data);
  252. return 0;
  253. }
  254. static bool is_volatile_reg(struct device *dev, unsigned int reg)
  255. {
  256. struct tps65910 *tps65910 = dev_get_drvdata(dev);
  257. /*
  258. * Caching all regulator registers.
  259. * All regualator register address range is same for
  260. * TPS65910 and TPS65911
  261. */
  262. if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
  263. /* Check for non-existing register */
  264. if (tps65910_chip_id(tps65910) == TPS65910)
  265. if ((reg == TPS65911_VDDCTRL_OP) ||
  266. (reg == TPS65911_VDDCTRL_SR))
  267. return true;
  268. return false;
  269. }
  270. return true;
  271. }
  272. static const struct regmap_config tps65910_regmap_config = {
  273. .reg_bits = 8,
  274. .val_bits = 8,
  275. .volatile_reg = is_volatile_reg,
  276. .max_register = TPS65910_MAX_REGISTER - 1,
  277. .cache_type = REGCACHE_RBTREE,
  278. };
  279. static int tps65910_ck32k_init(struct tps65910 *tps65910,
  280. struct tps65910_board *pmic_pdata)
  281. {
  282. int ret;
  283. if (!pmic_pdata->en_ck32k_xtal)
  284. return 0;
  285. ret = tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  286. DEVCTRL_CK32K_CTRL_MASK);
  287. if (ret < 0) {
  288. dev_err(tps65910->dev, "clear ck32k_ctrl failed: %d\n", ret);
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. static int tps65910_sleepinit(struct tps65910 *tps65910,
  294. struct tps65910_board *pmic_pdata)
  295. {
  296. struct device *dev = NULL;
  297. int ret = 0;
  298. dev = tps65910->dev;
  299. if (!pmic_pdata->en_dev_slp)
  300. return 0;
  301. /* enabling SLEEP device state */
  302. ret = tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  303. DEVCTRL_DEV_SLP_MASK);
  304. if (ret < 0) {
  305. dev_err(dev, "set dev_slp failed: %d\n", ret);
  306. goto err_sleep_init;
  307. }
  308. /* Return if there is no sleep keepon data. */
  309. if (!pmic_pdata->slp_keepon)
  310. return 0;
  311. if (pmic_pdata->slp_keepon->therm_keepon) {
  312. ret = tps65910_reg_set_bits(tps65910,
  313. TPS65910_SLEEP_KEEP_RES_ON,
  314. SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
  315. if (ret < 0) {
  316. dev_err(dev, "set therm_keepon failed: %d\n", ret);
  317. goto disable_dev_slp;
  318. }
  319. }
  320. if (pmic_pdata->slp_keepon->clkout32k_keepon) {
  321. ret = tps65910_reg_set_bits(tps65910,
  322. TPS65910_SLEEP_KEEP_RES_ON,
  323. SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
  324. if (ret < 0) {
  325. dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
  326. goto disable_dev_slp;
  327. }
  328. }
  329. if (pmic_pdata->slp_keepon->i2chs_keepon) {
  330. ret = tps65910_reg_set_bits(tps65910,
  331. TPS65910_SLEEP_KEEP_RES_ON,
  332. SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
  333. if (ret < 0) {
  334. dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
  335. goto disable_dev_slp;
  336. }
  337. }
  338. return 0;
  339. disable_dev_slp:
  340. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  341. DEVCTRL_DEV_SLP_MASK);
  342. err_sleep_init:
  343. return ret;
  344. }
  345. #ifdef CONFIG_OF
  346. static const struct of_device_id tps65910_of_match[] = {
  347. { .compatible = "ti,tps65910", .data = (void *)TPS65910},
  348. { .compatible = "ti,tps65911", .data = (void *)TPS65911},
  349. { },
  350. };
  351. MODULE_DEVICE_TABLE(of, tps65910_of_match);
  352. static struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  353. unsigned long *chip_id)
  354. {
  355. struct device_node *np = client->dev.of_node;
  356. struct tps65910_board *board_info;
  357. unsigned int prop;
  358. const struct of_device_id *match;
  359. int ret = 0;
  360. match = of_match_device(tps65910_of_match, &client->dev);
  361. if (!match) {
  362. dev_err(&client->dev, "Failed to find matching dt id\n");
  363. return NULL;
  364. }
  365. *chip_id = (unsigned long)match->data;
  366. board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
  367. GFP_KERNEL);
  368. if (!board_info) {
  369. dev_err(&client->dev, "Failed to allocate pdata\n");
  370. return NULL;
  371. }
  372. ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
  373. if (!ret)
  374. board_info->vmbch_threshold = prop;
  375. ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
  376. if (!ret)
  377. board_info->vmbch2_threshold = prop;
  378. prop = of_property_read_bool(np, "ti,en-ck32k-xtal");
  379. board_info->en_ck32k_xtal = prop;
  380. board_info->irq = client->irq;
  381. board_info->irq_base = -1;
  382. board_info->pm_off = of_property_read_bool(np,
  383. "ti,system-power-controller");
  384. return board_info;
  385. }
  386. #else
  387. static inline
  388. struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
  389. unsigned long *chip_id)
  390. {
  391. return NULL;
  392. }
  393. #endif
  394. static struct i2c_client *tps65910_i2c_client;
  395. static void tps65910_power_off(void)
  396. {
  397. struct tps65910 *tps65910;
  398. tps65910 = dev_get_drvdata(&tps65910_i2c_client->dev);
  399. if (tps65910_reg_set_bits(tps65910, TPS65910_DEVCTRL,
  400. DEVCTRL_PWR_OFF_MASK) < 0)
  401. return;
  402. tps65910_reg_clear_bits(tps65910, TPS65910_DEVCTRL,
  403. DEVCTRL_DEV_ON_MASK);
  404. }
  405. static int tps65910_i2c_probe(struct i2c_client *i2c,
  406. const struct i2c_device_id *id)
  407. {
  408. struct tps65910 *tps65910;
  409. struct tps65910_board *pmic_plat_data;
  410. struct tps65910_board *of_pmic_plat_data = NULL;
  411. struct tps65910_platform_data *init_data;
  412. unsigned long chip_id = id->driver_data;
  413. int ret = 0;
  414. pmic_plat_data = dev_get_platdata(&i2c->dev);
  415. if (!pmic_plat_data && i2c->dev.of_node) {
  416. pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
  417. of_pmic_plat_data = pmic_plat_data;
  418. }
  419. if (!pmic_plat_data)
  420. return -EINVAL;
  421. init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
  422. if (init_data == NULL)
  423. return -ENOMEM;
  424. tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
  425. if (tps65910 == NULL)
  426. return -ENOMEM;
  427. tps65910->of_plat_data = of_pmic_plat_data;
  428. i2c_set_clientdata(i2c, tps65910);
  429. tps65910->dev = &i2c->dev;
  430. tps65910->i2c_client = i2c;
  431. tps65910->id = chip_id;
  432. /* Work around silicon erratum SWCZ010: the tps65910 may miss the
  433. * first I2C transfer. So issue a dummy transfer before the first
  434. * real transfer.
  435. */
  436. i2c_master_send(i2c, "", 1);
  437. tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
  438. if (IS_ERR(tps65910->regmap)) {
  439. ret = PTR_ERR(tps65910->regmap);
  440. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  441. return ret;
  442. }
  443. init_data->irq = pmic_plat_data->irq;
  444. init_data->irq_base = pmic_plat_data->irq_base;
  445. tps65910_irq_init(tps65910, init_data->irq, init_data);
  446. tps65910_ck32k_init(tps65910, pmic_plat_data);
  447. tps65910_sleepinit(tps65910, pmic_plat_data);
  448. if (pmic_plat_data->pm_off && !pm_power_off) {
  449. tps65910_i2c_client = i2c;
  450. pm_power_off = tps65910_power_off;
  451. }
  452. ret = mfd_add_devices(tps65910->dev, -1,
  453. tps65910s, ARRAY_SIZE(tps65910s),
  454. NULL, 0,
  455. regmap_irq_get_domain(tps65910->irq_data));
  456. if (ret < 0) {
  457. dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
  458. tps65910_irq_exit(tps65910);
  459. return ret;
  460. }
  461. return ret;
  462. }
  463. static int tps65910_i2c_remove(struct i2c_client *i2c)
  464. {
  465. struct tps65910 *tps65910 = i2c_get_clientdata(i2c);
  466. tps65910_irq_exit(tps65910);
  467. mfd_remove_devices(tps65910->dev);
  468. return 0;
  469. }
  470. static const struct i2c_device_id tps65910_i2c_id[] = {
  471. { "tps65910", TPS65910 },
  472. { "tps65911", TPS65911 },
  473. { }
  474. };
  475. MODULE_DEVICE_TABLE(i2c, tps65910_i2c_id);
  476. static struct i2c_driver tps65910_i2c_driver = {
  477. .driver = {
  478. .name = "tps65910",
  479. .of_match_table = of_match_ptr(tps65910_of_match),
  480. },
  481. .probe = tps65910_i2c_probe,
  482. .remove = tps65910_i2c_remove,
  483. .id_table = tps65910_i2c_id,
  484. };
  485. static int __init tps65910_i2c_init(void)
  486. {
  487. return i2c_add_driver(&tps65910_i2c_driver);
  488. }
  489. /* init early so consumer devices can complete system boot */
  490. subsys_initcall(tps65910_i2c_init);
  491. static void __exit tps65910_i2c_exit(void)
  492. {
  493. i2c_del_driver(&tps65910_i2c_driver);
  494. }
  495. module_exit(tps65910_i2c_exit);
  496. MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
  497. MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
  498. MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
  499. MODULE_LICENSE("GPL");