tps65217.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * tps65217.c
  3. *
  4. * TPS65217 chip family multi-function driver
  5. *
  6. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/i2c.h>
  22. #include <linux/irq.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/regmap.h>
  30. #include <linux/slab.h>
  31. #include <linux/mfd/core.h>
  32. #include <linux/mfd/tps65217.h>
  33. static struct resource charger_resources[] = {
  34. DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_AC, "AC"),
  35. DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_USB, "USB"),
  36. };
  37. static struct resource pb_resources[] = {
  38. DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_PB, "PB"),
  39. };
  40. struct tps65217_irq {
  41. int mask;
  42. int interrupt;
  43. };
  44. static const struct tps65217_irq tps65217_irqs[] = {
  45. [TPS65217_IRQ_PB] = {
  46. .mask = TPS65217_INT_PBM,
  47. .interrupt = TPS65217_INT_PBI,
  48. },
  49. [TPS65217_IRQ_AC] = {
  50. .mask = TPS65217_INT_ACM,
  51. .interrupt = TPS65217_INT_ACI,
  52. },
  53. [TPS65217_IRQ_USB] = {
  54. .mask = TPS65217_INT_USBM,
  55. .interrupt = TPS65217_INT_USBI,
  56. },
  57. };
  58. static void tps65217_irq_lock(struct irq_data *data)
  59. {
  60. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  61. mutex_lock(&tps->irq_lock);
  62. }
  63. static void tps65217_irq_sync_unlock(struct irq_data *data)
  64. {
  65. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  66. int ret;
  67. ret = tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
  68. TPS65217_PROTECT_NONE);
  69. if (ret != 0)
  70. dev_err(tps->dev, "Failed to sync IRQ masks\n");
  71. mutex_unlock(&tps->irq_lock);
  72. }
  73. static inline const struct tps65217_irq *
  74. irq_to_tps65217_irq(struct tps65217 *tps, struct irq_data *data)
  75. {
  76. return &tps65217_irqs[data->hwirq];
  77. }
  78. static void tps65217_irq_enable(struct irq_data *data)
  79. {
  80. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  81. const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
  82. tps->irq_mask &= ~irq_data->mask;
  83. }
  84. static void tps65217_irq_disable(struct irq_data *data)
  85. {
  86. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  87. const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
  88. tps->irq_mask |= irq_data->mask;
  89. }
  90. static struct irq_chip tps65217_irq_chip = {
  91. .irq_bus_lock = tps65217_irq_lock,
  92. .irq_bus_sync_unlock = tps65217_irq_sync_unlock,
  93. .irq_enable = tps65217_irq_enable,
  94. .irq_disable = tps65217_irq_disable,
  95. };
  96. static struct mfd_cell tps65217s[] = {
  97. {
  98. .name = "tps65217-pmic",
  99. .of_compatible = "ti,tps65217-pmic",
  100. },
  101. {
  102. .name = "tps65217-bl",
  103. .of_compatible = "ti,tps65217-bl",
  104. },
  105. {
  106. .name = "tps65217-charger",
  107. .num_resources = ARRAY_SIZE(charger_resources),
  108. .resources = charger_resources,
  109. .of_compatible = "ti,tps65217-charger",
  110. },
  111. {
  112. .name = "tps65217-pwrbutton",
  113. .num_resources = ARRAY_SIZE(pb_resources),
  114. .resources = pb_resources,
  115. .of_compatible = "ti,tps65217-pwrbutton",
  116. },
  117. };
  118. static irqreturn_t tps65217_irq_thread(int irq, void *data)
  119. {
  120. struct tps65217 *tps = data;
  121. unsigned int status;
  122. bool handled = false;
  123. int i;
  124. int ret;
  125. ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status);
  126. if (ret < 0) {
  127. dev_err(tps->dev, "Failed to read IRQ status: %d\n",
  128. ret);
  129. return IRQ_NONE;
  130. }
  131. for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
  132. if (status & tps65217_irqs[i].interrupt) {
  133. handle_nested_irq(irq_find_mapping(tps->irq_domain, i));
  134. handled = true;
  135. }
  136. }
  137. if (handled)
  138. return IRQ_HANDLED;
  139. return IRQ_NONE;
  140. }
  141. static int tps65217_irq_map(struct irq_domain *h, unsigned int virq,
  142. irq_hw_number_t hw)
  143. {
  144. struct tps65217 *tps = h->host_data;
  145. irq_set_chip_data(virq, tps);
  146. irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq);
  147. irq_set_nested_thread(virq, 1);
  148. irq_set_parent(virq, tps->irq);
  149. irq_set_noprobe(virq);
  150. return 0;
  151. }
  152. static const struct irq_domain_ops tps65217_irq_domain_ops = {
  153. .map = tps65217_irq_map,
  154. };
  155. static int tps65217_irq_init(struct tps65217 *tps, int irq)
  156. {
  157. int ret;
  158. mutex_init(&tps->irq_lock);
  159. tps->irq = irq;
  160. /* Mask all interrupt sources */
  161. tps->irq_mask = (TPS65217_INT_RESERVEDM | TPS65217_INT_PBM
  162. | TPS65217_INT_ACM | TPS65217_INT_USBM);
  163. tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
  164. TPS65217_PROTECT_NONE);
  165. tps->irq_domain = irq_domain_add_linear(tps->dev->of_node,
  166. TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps);
  167. if (!tps->irq_domain) {
  168. dev_err(tps->dev, "Could not create IRQ domain\n");
  169. return -ENOMEM;
  170. }
  171. ret = devm_request_threaded_irq(tps->dev, irq, NULL,
  172. tps65217_irq_thread, IRQF_ONESHOT,
  173. "tps65217-irq", tps);
  174. if (ret) {
  175. dev_err(tps->dev, "Failed to request IRQ %d: %d\n",
  176. irq, ret);
  177. return ret;
  178. }
  179. return 0;
  180. }
  181. /**
  182. * tps65217_reg_read: Read a single tps65217 register.
  183. *
  184. * @tps: Device to read from.
  185. * @reg: Register to read.
  186. * @val: Contians the value
  187. */
  188. int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
  189. unsigned int *val)
  190. {
  191. return regmap_read(tps->regmap, reg, val);
  192. }
  193. EXPORT_SYMBOL_GPL(tps65217_reg_read);
  194. /**
  195. * tps65217_reg_write: Write a single tps65217 register.
  196. *
  197. * @tps65217: Device to write to.
  198. * @reg: Register to write to.
  199. * @val: Value to write.
  200. * @level: Password protected level
  201. */
  202. int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
  203. unsigned int val, unsigned int level)
  204. {
  205. int ret;
  206. unsigned int xor_reg_val;
  207. switch (level) {
  208. case TPS65217_PROTECT_NONE:
  209. return regmap_write(tps->regmap, reg, val);
  210. case TPS65217_PROTECT_L1:
  211. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  212. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  213. xor_reg_val);
  214. if (ret < 0)
  215. return ret;
  216. return regmap_write(tps->regmap, reg, val);
  217. case TPS65217_PROTECT_L2:
  218. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  219. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  220. xor_reg_val);
  221. if (ret < 0)
  222. return ret;
  223. ret = regmap_write(tps->regmap, reg, val);
  224. if (ret < 0)
  225. return ret;
  226. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  227. xor_reg_val);
  228. if (ret < 0)
  229. return ret;
  230. return regmap_write(tps->regmap, reg, val);
  231. default:
  232. return -EINVAL;
  233. }
  234. }
  235. EXPORT_SYMBOL_GPL(tps65217_reg_write);
  236. /**
  237. * tps65217_update_bits: Modify bits w.r.t mask, val and level.
  238. *
  239. * @tps65217: Device to write to.
  240. * @reg: Register to read-write to.
  241. * @mask: Mask.
  242. * @val: Value to write.
  243. * @level: Password protected level
  244. */
  245. static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
  246. unsigned int mask, unsigned int val, unsigned int level)
  247. {
  248. int ret;
  249. unsigned int data;
  250. ret = tps65217_reg_read(tps, reg, &data);
  251. if (ret) {
  252. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  253. return ret;
  254. }
  255. data &= ~mask;
  256. data |= val & mask;
  257. ret = tps65217_reg_write(tps, reg, data, level);
  258. if (ret)
  259. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  260. return ret;
  261. }
  262. int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
  263. unsigned int mask, unsigned int val, unsigned int level)
  264. {
  265. return tps65217_update_bits(tps, reg, mask, val, level);
  266. }
  267. EXPORT_SYMBOL_GPL(tps65217_set_bits);
  268. int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
  269. unsigned int mask, unsigned int level)
  270. {
  271. return tps65217_update_bits(tps, reg, mask, 0, level);
  272. }
  273. EXPORT_SYMBOL_GPL(tps65217_clear_bits);
  274. static bool tps65217_volatile_reg(struct device *dev, unsigned int reg)
  275. {
  276. switch (reg) {
  277. case TPS65217_REG_INT:
  278. return true;
  279. default:
  280. return false;
  281. }
  282. }
  283. static const struct regmap_config tps65217_regmap_config = {
  284. .reg_bits = 8,
  285. .val_bits = 8,
  286. .max_register = TPS65217_REG_MAX,
  287. .volatile_reg = tps65217_volatile_reg,
  288. };
  289. static const struct of_device_id tps65217_of_match[] = {
  290. { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
  291. { /* sentinel */ },
  292. };
  293. MODULE_DEVICE_TABLE(of, tps65217_of_match);
  294. static int tps65217_probe(struct i2c_client *client,
  295. const struct i2c_device_id *ids)
  296. {
  297. struct tps65217 *tps;
  298. unsigned int version;
  299. unsigned long chip_id = ids->driver_data;
  300. const struct of_device_id *match;
  301. bool status_off = false;
  302. int ret;
  303. if (client->dev.of_node) {
  304. match = of_match_device(tps65217_of_match, &client->dev);
  305. if (!match) {
  306. dev_err(&client->dev,
  307. "Failed to find matching dt id\n");
  308. return -EINVAL;
  309. }
  310. chip_id = (unsigned long)match->data;
  311. status_off = of_property_read_bool(client->dev.of_node,
  312. "ti,pmic-shutdown-controller");
  313. }
  314. if (!chip_id) {
  315. dev_err(&client->dev, "id is null.\n");
  316. return -ENODEV;
  317. }
  318. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  319. if (!tps)
  320. return -ENOMEM;
  321. i2c_set_clientdata(client, tps);
  322. tps->dev = &client->dev;
  323. tps->id = chip_id;
  324. tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
  325. if (IS_ERR(tps->regmap)) {
  326. ret = PTR_ERR(tps->regmap);
  327. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  328. ret);
  329. return ret;
  330. }
  331. if (client->irq) {
  332. tps65217_irq_init(tps, client->irq);
  333. } else {
  334. int i;
  335. /* Don't tell children about IRQ resources which won't fire */
  336. for (i = 0; i < ARRAY_SIZE(tps65217s); i++)
  337. tps65217s[i].num_resources = 0;
  338. }
  339. ret = devm_mfd_add_devices(tps->dev, -1, tps65217s,
  340. ARRAY_SIZE(tps65217s), NULL, 0,
  341. tps->irq_domain);
  342. if (ret < 0) {
  343. dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
  344. return ret;
  345. }
  346. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  347. if (ret < 0) {
  348. dev_err(tps->dev, "Failed to read revision register: %d\n",
  349. ret);
  350. return ret;
  351. }
  352. /* Set the PMIC to shutdown on PWR_EN toggle */
  353. if (status_off) {
  354. ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
  355. TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
  356. TPS65217_PROTECT_NONE);
  357. if (ret)
  358. dev_warn(tps->dev, "unable to set the status OFF\n");
  359. }
  360. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  361. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  362. version & TPS65217_CHIPID_REV_MASK);
  363. return 0;
  364. }
  365. static int tps65217_remove(struct i2c_client *client)
  366. {
  367. struct tps65217 *tps = i2c_get_clientdata(client);
  368. unsigned int virq;
  369. int i;
  370. for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
  371. virq = irq_find_mapping(tps->irq_domain, i);
  372. if (virq)
  373. irq_dispose_mapping(virq);
  374. }
  375. irq_domain_remove(tps->irq_domain);
  376. tps->irq_domain = NULL;
  377. return 0;
  378. }
  379. static const struct i2c_device_id tps65217_id_table[] = {
  380. {"tps65217", TPS65217},
  381. { /* sentinel */ }
  382. };
  383. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  384. static struct i2c_driver tps65217_driver = {
  385. .driver = {
  386. .name = "tps65217",
  387. .of_match_table = tps65217_of_match,
  388. },
  389. .id_table = tps65217_id_table,
  390. .probe = tps65217_probe,
  391. .remove = tps65217_remove,
  392. };
  393. static int __init tps65217_init(void)
  394. {
  395. return i2c_add_driver(&tps65217_driver);
  396. }
  397. subsys_initcall(tps65217_init);
  398. static void __exit tps65217_exit(void)
  399. {
  400. i2c_del_driver(&tps65217_driver);
  401. }
  402. module_exit(tps65217_exit);
  403. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  404. MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
  405. MODULE_LICENSE("GPL v2");