rc5t583.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Core driver access RC5T583 power management chip.
  3. *
  4. * Copyright (c) 2011-2012, NVIDIA CORPORATION. All rights reserved.
  5. * Author: Laxman dewangan <ldewangan@nvidia.com>
  6. *
  7. * Based on code
  8. * Copyright (C) 2011 RICOH COMPANY,LTD
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #include <linux/interrupt.h>
  24. #include <linux/irq.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/err.h>
  29. #include <linux/slab.h>
  30. #include <linux/i2c.h>
  31. #include <linux/mfd/core.h>
  32. #include <linux/mfd/rc5t583.h>
  33. #include <linux/regmap.h>
  34. #define RICOH_ONOFFSEL_REG 0x10
  35. #define RICOH_SWCTL_REG 0x5E
  36. struct deepsleep_control_data {
  37. u8 reg_add;
  38. u8 ds_pos_bit;
  39. };
  40. #define DEEPSLEEP_INIT(_id, _reg, _pos) \
  41. { \
  42. .reg_add = RC5T583_##_reg, \
  43. .ds_pos_bit = _pos, \
  44. }
  45. static struct deepsleep_control_data deepsleep_data[] = {
  46. DEEPSLEEP_INIT(DC0, SLPSEQ1, 0),
  47. DEEPSLEEP_INIT(DC1, SLPSEQ1, 4),
  48. DEEPSLEEP_INIT(DC2, SLPSEQ2, 0),
  49. DEEPSLEEP_INIT(DC3, SLPSEQ2, 4),
  50. DEEPSLEEP_INIT(LDO0, SLPSEQ3, 0),
  51. DEEPSLEEP_INIT(LDO1, SLPSEQ3, 4),
  52. DEEPSLEEP_INIT(LDO2, SLPSEQ4, 0),
  53. DEEPSLEEP_INIT(LDO3, SLPSEQ4, 4),
  54. DEEPSLEEP_INIT(LDO4, SLPSEQ5, 0),
  55. DEEPSLEEP_INIT(LDO5, SLPSEQ5, 4),
  56. DEEPSLEEP_INIT(LDO6, SLPSEQ6, 0),
  57. DEEPSLEEP_INIT(LDO7, SLPSEQ6, 4),
  58. DEEPSLEEP_INIT(LDO8, SLPSEQ7, 0),
  59. DEEPSLEEP_INIT(LDO9, SLPSEQ7, 4),
  60. DEEPSLEEP_INIT(PSO0, SLPSEQ8, 0),
  61. DEEPSLEEP_INIT(PSO1, SLPSEQ8, 4),
  62. DEEPSLEEP_INIT(PSO2, SLPSEQ9, 0),
  63. DEEPSLEEP_INIT(PSO3, SLPSEQ9, 4),
  64. DEEPSLEEP_INIT(PSO4, SLPSEQ10, 0),
  65. DEEPSLEEP_INIT(PSO5, SLPSEQ10, 4),
  66. DEEPSLEEP_INIT(PSO6, SLPSEQ11, 0),
  67. DEEPSLEEP_INIT(PSO7, SLPSEQ11, 4),
  68. };
  69. #define EXT_PWR_REQ \
  70. (RC5T583_EXT_PWRREQ1_CONTROL | RC5T583_EXT_PWRREQ2_CONTROL)
  71. static const struct mfd_cell rc5t583_subdevs[] = {
  72. {.name = "rc5t583-gpio",},
  73. {.name = "rc5t583-regulator",},
  74. {.name = "rc5t583-rtc", },
  75. {.name = "rc5t583-key", }
  76. };
  77. static int __rc5t583_set_ext_pwrreq1_control(struct device *dev,
  78. int id, int ext_pwr, int slots)
  79. {
  80. int ret;
  81. uint8_t sleepseq_val = 0;
  82. unsigned int en_bit;
  83. unsigned int slot_bit;
  84. if (id == RC5T583_DS_DC0) {
  85. dev_err(dev, "PWRREQ1 is invalid control for rail %d\n", id);
  86. return -EINVAL;
  87. }
  88. en_bit = deepsleep_data[id].ds_pos_bit;
  89. slot_bit = en_bit + 1;
  90. ret = rc5t583_read(dev, deepsleep_data[id].reg_add, &sleepseq_val);
  91. if (ret < 0) {
  92. dev_err(dev, "Error in reading reg 0x%x\n",
  93. deepsleep_data[id].reg_add);
  94. return ret;
  95. }
  96. sleepseq_val &= ~(0xF << en_bit);
  97. sleepseq_val |= BIT(en_bit);
  98. sleepseq_val |= ((slots & 0x7) << slot_bit);
  99. ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(1));
  100. if (ret < 0) {
  101. dev_err(dev, "Error in updating the 0x%02x register\n",
  102. RICOH_ONOFFSEL_REG);
  103. return ret;
  104. }
  105. ret = rc5t583_write(dev, deepsleep_data[id].reg_add, sleepseq_val);
  106. if (ret < 0) {
  107. dev_err(dev, "Error in writing reg 0x%x\n",
  108. deepsleep_data[id].reg_add);
  109. return ret;
  110. }
  111. if (id == RC5T583_DS_LDO4) {
  112. ret = rc5t583_write(dev, RICOH_SWCTL_REG, 0x1);
  113. if (ret < 0)
  114. dev_err(dev, "Error in writing reg 0x%x\n",
  115. RICOH_SWCTL_REG);
  116. }
  117. return ret;
  118. }
  119. static int __rc5t583_set_ext_pwrreq2_control(struct device *dev,
  120. int id, int ext_pwr)
  121. {
  122. int ret;
  123. if (id != RC5T583_DS_DC0) {
  124. dev_err(dev, "PWRREQ2 is invalid control for rail %d\n", id);
  125. return -EINVAL;
  126. }
  127. ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(2));
  128. if (ret < 0)
  129. dev_err(dev, "Error in updating the ONOFFSEL 0x10 register\n");
  130. return ret;
  131. }
  132. int rc5t583_ext_power_req_config(struct device *dev, int ds_id,
  133. int ext_pwr_req, int deepsleep_slot_nr)
  134. {
  135. if ((ext_pwr_req & EXT_PWR_REQ) == EXT_PWR_REQ)
  136. return -EINVAL;
  137. if (ext_pwr_req & RC5T583_EXT_PWRREQ1_CONTROL)
  138. return __rc5t583_set_ext_pwrreq1_control(dev, ds_id,
  139. ext_pwr_req, deepsleep_slot_nr);
  140. if (ext_pwr_req & RC5T583_EXT_PWRREQ2_CONTROL)
  141. return __rc5t583_set_ext_pwrreq2_control(dev,
  142. ds_id, ext_pwr_req);
  143. return 0;
  144. }
  145. EXPORT_SYMBOL(rc5t583_ext_power_req_config);
  146. static int rc5t583_clear_ext_power_req(struct rc5t583 *rc5t583,
  147. struct rc5t583_platform_data *pdata)
  148. {
  149. int ret;
  150. int i;
  151. uint8_t on_off_val = 0;
  152. /* Clear ONOFFSEL register */
  153. if (pdata->enable_shutdown)
  154. on_off_val = 0x1;
  155. ret = rc5t583_write(rc5t583->dev, RICOH_ONOFFSEL_REG, on_off_val);
  156. if (ret < 0)
  157. dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
  158. RICOH_ONOFFSEL_REG, ret);
  159. ret = rc5t583_write(rc5t583->dev, RICOH_SWCTL_REG, 0x0);
  160. if (ret < 0)
  161. dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
  162. RICOH_SWCTL_REG, ret);
  163. /* Clear sleep sequence register */
  164. for (i = RC5T583_SLPSEQ1; i <= RC5T583_SLPSEQ11; ++i) {
  165. ret = rc5t583_write(rc5t583->dev, i, 0x0);
  166. if (ret < 0)
  167. dev_warn(rc5t583->dev,
  168. "Error in writing reg 0x%02x error: %d\n",
  169. i, ret);
  170. }
  171. return 0;
  172. }
  173. static bool volatile_reg(struct device *dev, unsigned int reg)
  174. {
  175. /* Enable caching in interrupt registers */
  176. switch (reg) {
  177. case RC5T583_INT_EN_SYS1:
  178. case RC5T583_INT_EN_SYS2:
  179. case RC5T583_INT_EN_DCDC:
  180. case RC5T583_INT_EN_RTC:
  181. case RC5T583_INT_EN_ADC1:
  182. case RC5T583_INT_EN_ADC2:
  183. case RC5T583_INT_EN_ADC3:
  184. case RC5T583_GPIO_GPEDGE1:
  185. case RC5T583_GPIO_GPEDGE2:
  186. case RC5T583_GPIO_EN_INT:
  187. return false;
  188. case RC5T583_GPIO_MON_IOIN:
  189. /* This is gpio input register */
  190. return true;
  191. default:
  192. /* Enable caching in gpio registers */
  193. if ((reg >= RC5T583_GPIO_IOSEL) &&
  194. (reg <= RC5T583_GPIO_GPOFUNC))
  195. return false;
  196. /* Enable caching in sleep seq registers */
  197. if ((reg >= RC5T583_SLPSEQ1) && (reg <= RC5T583_SLPSEQ11))
  198. return false;
  199. /* Enable caching of regulator registers */
  200. if ((reg >= RC5T583_REG_DC0CTL) && (reg <= RC5T583_REG_SR3CTL))
  201. return false;
  202. if ((reg >= RC5T583_REG_LDOEN1) &&
  203. (reg <= RC5T583_REG_LDO9DAC_DS))
  204. return false;
  205. break;
  206. }
  207. return true;
  208. }
  209. static const struct regmap_config rc5t583_regmap_config = {
  210. .reg_bits = 8,
  211. .val_bits = 8,
  212. .volatile_reg = volatile_reg,
  213. .max_register = RC5T583_MAX_REG,
  214. .num_reg_defaults_raw = RC5T583_NUM_REGS,
  215. .cache_type = REGCACHE_RBTREE,
  216. };
  217. static int rc5t583_i2c_probe(struct i2c_client *i2c,
  218. const struct i2c_device_id *id)
  219. {
  220. struct rc5t583 *rc5t583;
  221. struct rc5t583_platform_data *pdata = dev_get_platdata(&i2c->dev);
  222. int ret;
  223. if (!pdata) {
  224. dev_err(&i2c->dev, "Err: Platform data not found\n");
  225. return -EINVAL;
  226. }
  227. rc5t583 = devm_kzalloc(&i2c->dev, sizeof(struct rc5t583), GFP_KERNEL);
  228. if (!rc5t583) {
  229. dev_err(&i2c->dev, "Memory allocation failed\n");
  230. return -ENOMEM;
  231. }
  232. rc5t583->dev = &i2c->dev;
  233. i2c_set_clientdata(i2c, rc5t583);
  234. rc5t583->regmap = devm_regmap_init_i2c(i2c, &rc5t583_regmap_config);
  235. if (IS_ERR(rc5t583->regmap)) {
  236. ret = PTR_ERR(rc5t583->regmap);
  237. dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
  238. return ret;
  239. }
  240. ret = rc5t583_clear_ext_power_req(rc5t583, pdata);
  241. if (ret < 0)
  242. return ret;
  243. if (i2c->irq) {
  244. ret = rc5t583_irq_init(rc5t583, i2c->irq, pdata->irq_base);
  245. /* Still continue with warning, if irq init fails */
  246. if (ret)
  247. dev_warn(&i2c->dev, "IRQ init failed: %d\n", ret);
  248. }
  249. ret = devm_mfd_add_devices(rc5t583->dev, -1, rc5t583_subdevs,
  250. ARRAY_SIZE(rc5t583_subdevs), NULL, 0, NULL);
  251. if (ret) {
  252. dev_err(&i2c->dev, "add mfd devices failed: %d\n", ret);
  253. return ret;
  254. }
  255. return 0;
  256. }
  257. static const struct i2c_device_id rc5t583_i2c_id[] = {
  258. {.name = "rc5t583", .driver_data = 0},
  259. {}
  260. };
  261. MODULE_DEVICE_TABLE(i2c, rc5t583_i2c_id);
  262. static struct i2c_driver rc5t583_i2c_driver = {
  263. .driver = {
  264. .name = "rc5t583",
  265. },
  266. .probe = rc5t583_i2c_probe,
  267. .id_table = rc5t583_i2c_id,
  268. };
  269. static int __init rc5t583_i2c_init(void)
  270. {
  271. return i2c_add_driver(&rc5t583_i2c_driver);
  272. }
  273. subsys_initcall(rc5t583_i2c_init);
  274. static void __exit rc5t583_i2c_exit(void)
  275. {
  276. i2c_del_driver(&rc5t583_i2c_driver);
  277. }
  278. module_exit(rc5t583_i2c_exit);
  279. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  280. MODULE_DESCRIPTION("RICOH RC5T583 power management system device driver");
  281. MODULE_LICENSE("GPL v2");