tps65217.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. * tps65217.c
  3. *
  4. * TPS65217 chip family multi-function driver
  5. *
  6. * Copyright (C) 2011 Texas Instruments Incorporated - https://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. static void tps65217_irq_lock(struct irq_data *data)
  41. {
  42. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  43. mutex_lock(&tps->irq_lock);
  44. }
  45. static void tps65217_irq_sync_unlock(struct irq_data *data)
  46. {
  47. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  48. int ret;
  49. ret = tps65217_set_bits(tps, TPS65217_REG_INT, TPS65217_INT_MASK,
  50. tps->irq_mask, TPS65217_PROTECT_NONE);
  51. if (ret != 0)
  52. dev_err(tps->dev, "Failed to sync IRQ masks\n");
  53. mutex_unlock(&tps->irq_lock);
  54. }
  55. static void tps65217_irq_enable(struct irq_data *data)
  56. {
  57. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  58. u8 mask = BIT(data->hwirq) << TPS65217_INT_SHIFT;
  59. tps->irq_mask &= ~mask;
  60. }
  61. static void tps65217_irq_disable(struct irq_data *data)
  62. {
  63. struct tps65217 *tps = irq_data_get_irq_chip_data(data);
  64. u8 mask = BIT(data->hwirq) << TPS65217_INT_SHIFT;
  65. tps->irq_mask |= mask;
  66. }
  67. static struct irq_chip tps65217_irq_chip = {
  68. .name = "tps65217",
  69. .irq_bus_lock = tps65217_irq_lock,
  70. .irq_bus_sync_unlock = tps65217_irq_sync_unlock,
  71. .irq_enable = tps65217_irq_enable,
  72. .irq_disable = tps65217_irq_disable,
  73. };
  74. static struct mfd_cell tps65217s[] = {
  75. {
  76. .name = "tps65217-pmic",
  77. .of_compatible = "ti,tps65217-pmic",
  78. },
  79. {
  80. .name = "tps65217-bl",
  81. .of_compatible = "ti,tps65217-bl",
  82. },
  83. {
  84. .name = "tps65217-charger",
  85. .num_resources = ARRAY_SIZE(charger_resources),
  86. .resources = charger_resources,
  87. .of_compatible = "ti,tps65217-charger",
  88. },
  89. {
  90. .name = "tps65217-pwrbutton",
  91. .num_resources = ARRAY_SIZE(pb_resources),
  92. .resources = pb_resources,
  93. .of_compatible = "ti,tps65217-pwrbutton",
  94. },
  95. };
  96. static irqreturn_t tps65217_irq_thread(int irq, void *data)
  97. {
  98. struct tps65217 *tps = data;
  99. unsigned int status;
  100. bool handled = false;
  101. int i;
  102. int ret;
  103. ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status);
  104. if (ret < 0) {
  105. dev_err(tps->dev, "Failed to read IRQ status: %d\n",
  106. ret);
  107. return IRQ_NONE;
  108. }
  109. for (i = 0; i < TPS65217_NUM_IRQ; i++) {
  110. if (status & BIT(i)) {
  111. handle_nested_irq(irq_find_mapping(tps->irq_domain, i));
  112. handled = true;
  113. }
  114. }
  115. if (handled)
  116. return IRQ_HANDLED;
  117. return IRQ_NONE;
  118. }
  119. static int tps65217_irq_map(struct irq_domain *h, unsigned int virq,
  120. irq_hw_number_t hw)
  121. {
  122. struct tps65217 *tps = h->host_data;
  123. irq_set_chip_data(virq, tps);
  124. irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq);
  125. irq_set_nested_thread(virq, 1);
  126. irq_set_parent(virq, tps->irq);
  127. irq_set_noprobe(virq);
  128. return 0;
  129. }
  130. static const struct irq_domain_ops tps65217_irq_domain_ops = {
  131. .map = tps65217_irq_map,
  132. };
  133. static int tps65217_irq_init(struct tps65217 *tps, int irq)
  134. {
  135. int ret;
  136. mutex_init(&tps->irq_lock);
  137. tps->irq = irq;
  138. /* Mask all interrupt sources */
  139. tps->irq_mask = TPS65217_INT_MASK;
  140. tps65217_set_bits(tps, TPS65217_REG_INT, TPS65217_INT_MASK,
  141. TPS65217_INT_MASK, TPS65217_PROTECT_NONE);
  142. tps->irq_domain = irq_domain_add_linear(tps->dev->of_node,
  143. TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps);
  144. if (!tps->irq_domain) {
  145. dev_err(tps->dev, "Could not create IRQ domain\n");
  146. return -ENOMEM;
  147. }
  148. ret = devm_request_threaded_irq(tps->dev, irq, NULL,
  149. tps65217_irq_thread, IRQF_ONESHOT,
  150. "tps65217-irq", tps);
  151. if (ret) {
  152. dev_err(tps->dev, "Failed to request IRQ %d: %d\n",
  153. irq, ret);
  154. return ret;
  155. }
  156. enable_irq_wake(irq);
  157. return 0;
  158. }
  159. /**
  160. * tps65217_reg_read: Read a single tps65217 register.
  161. *
  162. * @tps: Device to read from.
  163. * @reg: Register to read.
  164. * @val: Contians the value
  165. */
  166. int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
  167. unsigned int *val)
  168. {
  169. return regmap_read(tps->regmap, reg, val);
  170. }
  171. EXPORT_SYMBOL_GPL(tps65217_reg_read);
  172. /**
  173. * tps65217_reg_write: Write a single tps65217 register.
  174. *
  175. * @tps: Device to write to.
  176. * @reg: Register to write to.
  177. * @val: Value to write.
  178. * @level: Password protected level
  179. */
  180. int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
  181. unsigned int val, unsigned int level)
  182. {
  183. int ret;
  184. unsigned int xor_reg_val;
  185. switch (level) {
  186. case TPS65217_PROTECT_NONE:
  187. return regmap_write(tps->regmap, reg, val);
  188. case TPS65217_PROTECT_L1:
  189. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  190. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  191. xor_reg_val);
  192. if (ret < 0)
  193. return ret;
  194. return regmap_write(tps->regmap, reg, val);
  195. case TPS65217_PROTECT_L2:
  196. xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
  197. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  198. xor_reg_val);
  199. if (ret < 0)
  200. return ret;
  201. ret = regmap_write(tps->regmap, reg, val);
  202. if (ret < 0)
  203. return ret;
  204. ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
  205. xor_reg_val);
  206. if (ret < 0)
  207. return ret;
  208. return regmap_write(tps->regmap, reg, val);
  209. default:
  210. return -EINVAL;
  211. }
  212. }
  213. EXPORT_SYMBOL_GPL(tps65217_reg_write);
  214. /**
  215. * tps65217_update_bits: Modify bits w.r.t mask, val and level.
  216. *
  217. * @tps: Device to write to.
  218. * @reg: Register to read-write to.
  219. * @mask: Mask.
  220. * @val: Value to write.
  221. * @level: Password protected level
  222. */
  223. static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
  224. unsigned int mask, unsigned int val, unsigned int level)
  225. {
  226. int ret;
  227. unsigned int data;
  228. ret = tps65217_reg_read(tps, reg, &data);
  229. if (ret) {
  230. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  231. return ret;
  232. }
  233. data &= ~mask;
  234. data |= val & mask;
  235. ret = tps65217_reg_write(tps, reg, data, level);
  236. if (ret)
  237. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  238. return ret;
  239. }
  240. int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
  241. unsigned int mask, unsigned int val, unsigned int level)
  242. {
  243. return tps65217_update_bits(tps, reg, mask, val, level);
  244. }
  245. EXPORT_SYMBOL_GPL(tps65217_set_bits);
  246. int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
  247. unsigned int mask, unsigned int level)
  248. {
  249. return tps65217_update_bits(tps, reg, mask, 0, level);
  250. }
  251. EXPORT_SYMBOL_GPL(tps65217_clear_bits);
  252. static bool tps65217_volatile_reg(struct device *dev, unsigned int reg)
  253. {
  254. switch (reg) {
  255. case TPS65217_REG_INT:
  256. return true;
  257. default:
  258. return false;
  259. }
  260. }
  261. static const struct regmap_config tps65217_regmap_config = {
  262. .reg_bits = 8,
  263. .val_bits = 8,
  264. .max_register = TPS65217_REG_MAX,
  265. .volatile_reg = tps65217_volatile_reg,
  266. };
  267. static const struct of_device_id tps65217_of_match[] = {
  268. { .compatible = "ti,tps65217"},
  269. { /* sentinel */ },
  270. };
  271. MODULE_DEVICE_TABLE(of, tps65217_of_match);
  272. static int tps65217_probe(struct i2c_client *client)
  273. {
  274. struct tps65217 *tps;
  275. unsigned int version;
  276. bool status_off = false;
  277. int ret;
  278. status_off = of_property_read_bool(client->dev.of_node,
  279. "ti,pmic-shutdown-controller");
  280. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  281. if (!tps)
  282. return -ENOMEM;
  283. i2c_set_clientdata(client, tps);
  284. tps->dev = &client->dev;
  285. tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
  286. if (IS_ERR(tps->regmap)) {
  287. ret = PTR_ERR(tps->regmap);
  288. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  289. ret);
  290. return ret;
  291. }
  292. if (client->irq) {
  293. tps65217_irq_init(tps, client->irq);
  294. } else {
  295. int i;
  296. /* Don't tell children about IRQ resources which won't fire */
  297. for (i = 0; i < ARRAY_SIZE(tps65217s); i++)
  298. tps65217s[i].num_resources = 0;
  299. }
  300. ret = devm_mfd_add_devices(tps->dev, -1, tps65217s,
  301. ARRAY_SIZE(tps65217s), NULL, 0,
  302. tps->irq_domain);
  303. if (ret < 0) {
  304. dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
  305. return ret;
  306. }
  307. ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
  308. if (ret < 0) {
  309. dev_err(tps->dev, "Failed to read revision register: %d\n",
  310. ret);
  311. return ret;
  312. }
  313. /* Set the PMIC to shutdown on PWR_EN toggle */
  314. if (status_off) {
  315. ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
  316. TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
  317. TPS65217_PROTECT_NONE);
  318. if (ret)
  319. dev_warn(tps->dev, "unable to set the status OFF\n");
  320. }
  321. dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
  322. (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
  323. version & TPS65217_CHIPID_REV_MASK);
  324. return 0;
  325. }
  326. static int tps65217_remove(struct i2c_client *client)
  327. {
  328. struct tps65217 *tps = i2c_get_clientdata(client);
  329. unsigned int virq;
  330. int i;
  331. for (i = 0; i < TPS65217_NUM_IRQ; i++) {
  332. virq = irq_find_mapping(tps->irq_domain, i);
  333. if (virq)
  334. irq_dispose_mapping(virq);
  335. }
  336. irq_domain_remove(tps->irq_domain);
  337. tps->irq_domain = NULL;
  338. return 0;
  339. }
  340. static const struct i2c_device_id tps65217_id_table[] = {
  341. {"tps65217", TPS65217},
  342. { /* sentinel */ }
  343. };
  344. MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
  345. static struct i2c_driver tps65217_driver = {
  346. .driver = {
  347. .name = "tps65217",
  348. .of_match_table = tps65217_of_match,
  349. },
  350. .id_table = tps65217_id_table,
  351. .probe_new = tps65217_probe,
  352. .remove = tps65217_remove,
  353. };
  354. static int __init tps65217_init(void)
  355. {
  356. return i2c_add_driver(&tps65217_driver);
  357. }
  358. subsys_initcall(tps65217_init);
  359. static void __exit tps65217_exit(void)
  360. {
  361. i2c_del_driver(&tps65217_driver);
  362. }
  363. module_exit(tps65217_exit);
  364. MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
  365. MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
  366. MODULE_LICENSE("GPL v2");