tps65218.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Driver for TPS65218 Integrated power management chipsets
  3. *
  4. * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether expressed or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License version 2 for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/device.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/init.h>
  20. #include <linux/i2c.h>
  21. #include <linux/slab.h>
  22. #include <linux/regmap.h>
  23. #include <linux/err.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/irq.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/mutex.h>
  29. #include <linux/mfd/core.h>
  30. #include <linux/mfd/tps65218.h>
  31. #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D
  32. /**
  33. * tps65218_reg_read: Read a single tps65218 register.
  34. *
  35. * @tps: Device to read from.
  36. * @reg: Register to read.
  37. * @val: Contians the value
  38. */
  39. int tps65218_reg_read(struct tps65218 *tps, unsigned int reg,
  40. unsigned int *val)
  41. {
  42. return regmap_read(tps->regmap, reg, val);
  43. }
  44. EXPORT_SYMBOL_GPL(tps65218_reg_read);
  45. /**
  46. * tps65218_reg_write: Write a single tps65218 register.
  47. *
  48. * @tps65218: Device to write to.
  49. * @reg: Register to write to.
  50. * @val: Value to write.
  51. * @level: Password protected level
  52. */
  53. int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
  54. unsigned int val, unsigned int level)
  55. {
  56. int ret;
  57. unsigned int xor_reg_val;
  58. switch (level) {
  59. case TPS65218_PROTECT_NONE:
  60. return regmap_write(tps->regmap, reg, val);
  61. case TPS65218_PROTECT_L1:
  62. xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
  63. ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
  64. xor_reg_val);
  65. if (ret < 0)
  66. return ret;
  67. return regmap_write(tps->regmap, reg, val);
  68. default:
  69. return -EINVAL;
  70. }
  71. }
  72. EXPORT_SYMBOL_GPL(tps65218_reg_write);
  73. /**
  74. * tps65218_update_bits: Modify bits w.r.t mask, val and level.
  75. *
  76. * @tps65218: Device to write to.
  77. * @reg: Register to read-write to.
  78. * @mask: Mask.
  79. * @val: Value to write.
  80. * @level: Password protected level
  81. */
  82. static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
  83. unsigned int mask, unsigned int val, unsigned int level)
  84. {
  85. int ret;
  86. unsigned int data;
  87. ret = tps65218_reg_read(tps, reg, &data);
  88. if (ret) {
  89. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  90. return ret;
  91. }
  92. data &= ~mask;
  93. data |= val & mask;
  94. mutex_lock(&tps->tps_lock);
  95. ret = tps65218_reg_write(tps, reg, data, level);
  96. if (ret)
  97. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  98. mutex_unlock(&tps->tps_lock);
  99. return ret;
  100. }
  101. int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
  102. unsigned int mask, unsigned int val, unsigned int level)
  103. {
  104. return tps65218_update_bits(tps, reg, mask, val, level);
  105. }
  106. EXPORT_SYMBOL_GPL(tps65218_set_bits);
  107. int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
  108. unsigned int mask, unsigned int level)
  109. {
  110. return tps65218_update_bits(tps, reg, mask, 0, level);
  111. }
  112. EXPORT_SYMBOL_GPL(tps65218_clear_bits);
  113. static const struct regmap_range tps65218_yes_ranges[] = {
  114. regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
  115. regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
  116. };
  117. static const struct regmap_access_table tps65218_volatile_table = {
  118. .yes_ranges = tps65218_yes_ranges,
  119. .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
  120. };
  121. static const struct regmap_config tps65218_regmap_config = {
  122. .reg_bits = 8,
  123. .val_bits = 8,
  124. .cache_type = REGCACHE_RBTREE,
  125. .volatile_table = &tps65218_volatile_table,
  126. };
  127. static const struct regmap_irq tps65218_irqs[] = {
  128. /* INT1 IRQs */
  129. [TPS65218_PRGC_IRQ] = {
  130. .mask = TPS65218_INT1_PRGC,
  131. },
  132. [TPS65218_CC_AQC_IRQ] = {
  133. .mask = TPS65218_INT1_CC_AQC,
  134. },
  135. [TPS65218_HOT_IRQ] = {
  136. .mask = TPS65218_INT1_HOT,
  137. },
  138. [TPS65218_PB_IRQ] = {
  139. .mask = TPS65218_INT1_PB,
  140. },
  141. [TPS65218_AC_IRQ] = {
  142. .mask = TPS65218_INT1_AC,
  143. },
  144. [TPS65218_VPRG_IRQ] = {
  145. .mask = TPS65218_INT1_VPRG,
  146. },
  147. [TPS65218_INVALID1_IRQ] = {
  148. },
  149. [TPS65218_INVALID2_IRQ] = {
  150. },
  151. /* INT2 IRQs*/
  152. [TPS65218_LS1_I_IRQ] = {
  153. .mask = TPS65218_INT2_LS1_I,
  154. .reg_offset = 1,
  155. },
  156. [TPS65218_LS2_I_IRQ] = {
  157. .mask = TPS65218_INT2_LS2_I,
  158. .reg_offset = 1,
  159. },
  160. [TPS65218_LS3_I_IRQ] = {
  161. .mask = TPS65218_INT2_LS3_I,
  162. .reg_offset = 1,
  163. },
  164. [TPS65218_LS1_F_IRQ] = {
  165. .mask = TPS65218_INT2_LS1_F,
  166. .reg_offset = 1,
  167. },
  168. [TPS65218_LS2_F_IRQ] = {
  169. .mask = TPS65218_INT2_LS2_F,
  170. .reg_offset = 1,
  171. },
  172. [TPS65218_LS3_F_IRQ] = {
  173. .mask = TPS65218_INT2_LS3_F,
  174. .reg_offset = 1,
  175. },
  176. [TPS65218_INVALID3_IRQ] = {
  177. },
  178. [TPS65218_INVALID4_IRQ] = {
  179. },
  180. };
  181. static struct regmap_irq_chip tps65218_irq_chip = {
  182. .name = "tps65218",
  183. .irqs = tps65218_irqs,
  184. .num_irqs = ARRAY_SIZE(tps65218_irqs),
  185. .num_regs = 2,
  186. .mask_base = TPS65218_REG_INT_MASK1,
  187. .status_base = TPS65218_REG_INT1,
  188. };
  189. static const struct of_device_id of_tps65218_match_table[] = {
  190. { .compatible = "ti,tps65218", },
  191. {}
  192. };
  193. MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
  194. static int tps65218_probe(struct i2c_client *client,
  195. const struct i2c_device_id *ids)
  196. {
  197. struct tps65218 *tps;
  198. const struct of_device_id *match;
  199. int ret;
  200. unsigned int chipid;
  201. match = of_match_device(of_tps65218_match_table, &client->dev);
  202. if (!match) {
  203. dev_err(&client->dev,
  204. "Failed to find matching dt id\n");
  205. return -EINVAL;
  206. }
  207. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  208. if (!tps)
  209. return -ENOMEM;
  210. i2c_set_clientdata(client, tps);
  211. tps->dev = &client->dev;
  212. tps->irq = client->irq;
  213. tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
  214. if (IS_ERR(tps->regmap)) {
  215. ret = PTR_ERR(tps->regmap);
  216. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  217. ret);
  218. return ret;
  219. }
  220. mutex_init(&tps->tps_lock);
  221. ret = regmap_add_irq_chip(tps->regmap, tps->irq,
  222. IRQF_ONESHOT, 0, &tps65218_irq_chip,
  223. &tps->irq_data);
  224. if (ret < 0)
  225. return ret;
  226. ret = tps65218_reg_read(tps, TPS65218_REG_CHIPID, &chipid);
  227. if (ret) {
  228. dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
  229. return ret;
  230. }
  231. tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
  232. ret = of_platform_populate(client->dev.of_node, NULL, NULL,
  233. &client->dev);
  234. if (ret < 0)
  235. goto err_irq;
  236. return 0;
  237. err_irq:
  238. regmap_del_irq_chip(tps->irq, tps->irq_data);
  239. return ret;
  240. }
  241. static int tps65218_remove(struct i2c_client *client)
  242. {
  243. struct tps65218 *tps = i2c_get_clientdata(client);
  244. regmap_del_irq_chip(tps->irq, tps->irq_data);
  245. return 0;
  246. }
  247. static const struct i2c_device_id tps65218_id_table[] = {
  248. { "tps65218", TPS65218 },
  249. { },
  250. };
  251. MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
  252. static struct i2c_driver tps65218_driver = {
  253. .driver = {
  254. .name = "tps65218",
  255. .of_match_table = of_tps65218_match_table,
  256. },
  257. .probe = tps65218_probe,
  258. .remove = tps65218_remove,
  259. .id_table = tps65218_id_table,
  260. };
  261. module_i2c_driver(tps65218_driver);
  262. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  263. MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
  264. MODULE_LICENSE("GPL v2");