tps65218.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. static const struct mfd_cell tps65218_cells[] = {
  33. {
  34. .name = "tps65218-pwrbutton",
  35. .of_compatible = "ti,tps65218-pwrbutton",
  36. },
  37. {
  38. .name = "tps65218-gpio",
  39. .of_compatible = "ti,tps65218-gpio",
  40. },
  41. { .name = "tps65218-regulator", },
  42. };
  43. /**
  44. * tps65218_reg_write: Write a single tps65218 register.
  45. *
  46. * @tps65218: Device to write to.
  47. * @reg: Register to write to.
  48. * @val: Value to write.
  49. * @level: Password protected level
  50. */
  51. int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
  52. unsigned int val, unsigned int level)
  53. {
  54. int ret;
  55. unsigned int xor_reg_val;
  56. switch (level) {
  57. case TPS65218_PROTECT_NONE:
  58. return regmap_write(tps->regmap, reg, val);
  59. case TPS65218_PROTECT_L1:
  60. xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
  61. ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
  62. xor_reg_val);
  63. if (ret < 0)
  64. return ret;
  65. return regmap_write(tps->regmap, reg, val);
  66. default:
  67. return -EINVAL;
  68. }
  69. }
  70. EXPORT_SYMBOL_GPL(tps65218_reg_write);
  71. /**
  72. * tps65218_update_bits: Modify bits w.r.t mask, val and level.
  73. *
  74. * @tps65218: Device to write to.
  75. * @reg: Register to read-write to.
  76. * @mask: Mask.
  77. * @val: Value to write.
  78. * @level: Password protected level
  79. */
  80. static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
  81. unsigned int mask, unsigned int val, unsigned int level)
  82. {
  83. int ret;
  84. unsigned int data;
  85. ret = regmap_read(tps->regmap, reg, &data);
  86. if (ret) {
  87. dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
  88. return ret;
  89. }
  90. data &= ~mask;
  91. data |= val & mask;
  92. mutex_lock(&tps->tps_lock);
  93. ret = tps65218_reg_write(tps, reg, data, level);
  94. if (ret)
  95. dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
  96. mutex_unlock(&tps->tps_lock);
  97. return ret;
  98. }
  99. int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
  100. unsigned int mask, unsigned int val, unsigned int level)
  101. {
  102. return tps65218_update_bits(tps, reg, mask, val, level);
  103. }
  104. EXPORT_SYMBOL_GPL(tps65218_set_bits);
  105. int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
  106. unsigned int mask, unsigned int level)
  107. {
  108. return tps65218_update_bits(tps, reg, mask, 0, level);
  109. }
  110. EXPORT_SYMBOL_GPL(tps65218_clear_bits);
  111. static const struct regmap_range tps65218_yes_ranges[] = {
  112. regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
  113. regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
  114. };
  115. static const struct regmap_access_table tps65218_volatile_table = {
  116. .yes_ranges = tps65218_yes_ranges,
  117. .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
  118. };
  119. static const struct regmap_config tps65218_regmap_config = {
  120. .reg_bits = 8,
  121. .val_bits = 8,
  122. .cache_type = REGCACHE_RBTREE,
  123. .volatile_table = &tps65218_volatile_table,
  124. };
  125. static const struct regmap_irq tps65218_irqs[] = {
  126. /* INT1 IRQs */
  127. [TPS65218_PRGC_IRQ] = {
  128. .mask = TPS65218_INT1_PRGC,
  129. },
  130. [TPS65218_CC_AQC_IRQ] = {
  131. .mask = TPS65218_INT1_CC_AQC,
  132. },
  133. [TPS65218_HOT_IRQ] = {
  134. .mask = TPS65218_INT1_HOT,
  135. },
  136. [TPS65218_PB_IRQ] = {
  137. .mask = TPS65218_INT1_PB,
  138. },
  139. [TPS65218_AC_IRQ] = {
  140. .mask = TPS65218_INT1_AC,
  141. },
  142. [TPS65218_VPRG_IRQ] = {
  143. .mask = TPS65218_INT1_VPRG,
  144. },
  145. [TPS65218_INVALID1_IRQ] = {
  146. },
  147. [TPS65218_INVALID2_IRQ] = {
  148. },
  149. /* INT2 IRQs*/
  150. [TPS65218_LS1_I_IRQ] = {
  151. .mask = TPS65218_INT2_LS1_I,
  152. .reg_offset = 1,
  153. },
  154. [TPS65218_LS2_I_IRQ] = {
  155. .mask = TPS65218_INT2_LS2_I,
  156. .reg_offset = 1,
  157. },
  158. [TPS65218_LS3_I_IRQ] = {
  159. .mask = TPS65218_INT2_LS3_I,
  160. .reg_offset = 1,
  161. },
  162. [TPS65218_LS1_F_IRQ] = {
  163. .mask = TPS65218_INT2_LS1_F,
  164. .reg_offset = 1,
  165. },
  166. [TPS65218_LS2_F_IRQ] = {
  167. .mask = TPS65218_INT2_LS2_F,
  168. .reg_offset = 1,
  169. },
  170. [TPS65218_LS3_F_IRQ] = {
  171. .mask = TPS65218_INT2_LS3_F,
  172. .reg_offset = 1,
  173. },
  174. [TPS65218_INVALID3_IRQ] = {
  175. },
  176. [TPS65218_INVALID4_IRQ] = {
  177. },
  178. };
  179. static struct regmap_irq_chip tps65218_irq_chip = {
  180. .name = "tps65218",
  181. .irqs = tps65218_irqs,
  182. .num_irqs = ARRAY_SIZE(tps65218_irqs),
  183. .num_regs = 2,
  184. .mask_base = TPS65218_REG_INT_MASK1,
  185. .status_base = TPS65218_REG_INT1,
  186. };
  187. static const struct of_device_id of_tps65218_match_table[] = {
  188. { .compatible = "ti,tps65218", },
  189. {}
  190. };
  191. MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
  192. static int tps65218_probe(struct i2c_client *client,
  193. const struct i2c_device_id *ids)
  194. {
  195. struct tps65218 *tps;
  196. int ret;
  197. unsigned int chipid;
  198. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  199. if (!tps)
  200. return -ENOMEM;
  201. i2c_set_clientdata(client, tps);
  202. tps->dev = &client->dev;
  203. tps->irq = client->irq;
  204. tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
  205. if (IS_ERR(tps->regmap)) {
  206. ret = PTR_ERR(tps->regmap);
  207. dev_err(tps->dev, "Failed to allocate register map: %d\n",
  208. ret);
  209. return ret;
  210. }
  211. mutex_init(&tps->tps_lock);
  212. ret = devm_regmap_add_irq_chip(&client->dev, tps->regmap, tps->irq,
  213. IRQF_ONESHOT, 0, &tps65218_irq_chip,
  214. &tps->irq_data);
  215. if (ret < 0)
  216. return ret;
  217. ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid);
  218. if (ret) {
  219. dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
  220. return ret;
  221. }
  222. tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
  223. ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells,
  224. ARRAY_SIZE(tps65218_cells), NULL, 0,
  225. regmap_irq_get_domain(tps->irq_data));
  226. return ret;
  227. }
  228. static const struct i2c_device_id tps65218_id_table[] = {
  229. { "tps65218", TPS65218 },
  230. { },
  231. };
  232. MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
  233. static struct i2c_driver tps65218_driver = {
  234. .driver = {
  235. .name = "tps65218",
  236. .of_match_table = of_tps65218_match_table,
  237. },
  238. .probe = tps65218_probe,
  239. .id_table = tps65218_id_table,
  240. };
  241. module_i2c_driver(tps65218_driver);
  242. MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
  243. MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
  244. MODULE_LICENSE("GPL v2");