rc5t583.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 struct mfd_cell rc5t583_subdevs[] = {
  72. {.name = "rc5t583-regulator",},
  73. {.name = "rc5t583-rtc", },
  74. {.name = "rc5t583-key", }
  75. };
  76. static int __rc5t583_set_ext_pwrreq1_control(struct device *dev,
  77. int id, int ext_pwr, int slots)
  78. {
  79. int ret;
  80. uint8_t sleepseq_val;
  81. unsigned int en_bit;
  82. unsigned int slot_bit;
  83. if (id == RC5T583_DS_DC0) {
  84. dev_err(dev, "PWRREQ1 is invalid control for rail %d\n", id);
  85. return -EINVAL;
  86. }
  87. en_bit = deepsleep_data[id].ds_pos_bit;
  88. slot_bit = en_bit + 1;
  89. ret = rc5t583_read(dev, deepsleep_data[id].reg_add, &sleepseq_val);
  90. if (ret < 0) {
  91. dev_err(dev, "Error in reading reg 0x%x\n",
  92. deepsleep_data[id].reg_add);
  93. return ret;
  94. }
  95. sleepseq_val &= ~(0xF << en_bit);
  96. sleepseq_val |= BIT(en_bit);
  97. sleepseq_val |= ((slots & 0x7) << slot_bit);
  98. ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(1));
  99. if (ret < 0) {
  100. dev_err(dev, "Error in updating the 0x%02x register\n",
  101. RICOH_ONOFFSEL_REG);
  102. return ret;
  103. }
  104. ret = rc5t583_write(dev, deepsleep_data[id].reg_add, sleepseq_val);
  105. if (ret < 0) {
  106. dev_err(dev, "Error in writing reg 0x%x\n",
  107. deepsleep_data[id].reg_add);
  108. return ret;
  109. }
  110. if (id == RC5T583_DS_LDO4) {
  111. ret = rc5t583_write(dev, RICOH_SWCTL_REG, 0x1);
  112. if (ret < 0)
  113. dev_err(dev, "Error in writing reg 0x%x\n",
  114. RICOH_SWCTL_REG);
  115. }
  116. return ret;
  117. }
  118. static int __rc5t583_set_ext_pwrreq2_control(struct device *dev,
  119. int id, int ext_pwr)
  120. {
  121. int ret;
  122. if (id != RC5T583_DS_DC0) {
  123. dev_err(dev, "PWRREQ2 is invalid control for rail %d\n", id);
  124. return -EINVAL;
  125. }
  126. ret = rc5t583_set_bits(dev, RICOH_ONOFFSEL_REG, BIT(2));
  127. if (ret < 0)
  128. dev_err(dev, "Error in updating the ONOFFSEL 0x10 register\n");
  129. return ret;
  130. }
  131. int rc5t583_ext_power_req_config(struct device *dev, int ds_id,
  132. int ext_pwr_req, int deepsleep_slot_nr)
  133. {
  134. if ((ext_pwr_req & EXT_PWR_REQ) == EXT_PWR_REQ)
  135. return -EINVAL;
  136. if (ext_pwr_req & RC5T583_EXT_PWRREQ1_CONTROL)
  137. return __rc5t583_set_ext_pwrreq1_control(dev, ds_id,
  138. ext_pwr_req, deepsleep_slot_nr);
  139. if (ext_pwr_req & RC5T583_EXT_PWRREQ2_CONTROL)
  140. return __rc5t583_set_ext_pwrreq2_control(dev,
  141. ds_id, ext_pwr_req);
  142. return 0;
  143. }
  144. EXPORT_SYMBOL(rc5t583_ext_power_req_config);
  145. static int rc5t583_clear_ext_power_req(struct rc5t583 *rc5t583,
  146. struct rc5t583_platform_data *pdata)
  147. {
  148. int ret;
  149. int i;
  150. uint8_t on_off_val = 0;
  151. /* Clear ONOFFSEL register */
  152. if (pdata->enable_shutdown)
  153. on_off_val = 0x1;
  154. ret = rc5t583_write(rc5t583->dev, RICOH_ONOFFSEL_REG, on_off_val);
  155. if (ret < 0)
  156. dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
  157. RICOH_ONOFFSEL_REG, ret);
  158. ret = rc5t583_write(rc5t583->dev, RICOH_SWCTL_REG, 0x0);
  159. if (ret < 0)
  160. dev_warn(rc5t583->dev, "Error in writing reg %d error: %d\n",
  161. RICOH_SWCTL_REG, ret);
  162. /* Clear sleep sequence register */
  163. for (i = RC5T583_SLPSEQ1; i <= RC5T583_SLPSEQ11; ++i) {
  164. ret = rc5t583_write(rc5t583->dev, i, 0x0);
  165. if (ret < 0)
  166. dev_warn(rc5t583->dev,
  167. "Error in writing reg 0x%02x error: %d\n",
  168. i, ret);
  169. }
  170. return 0;
  171. }
  172. static bool volatile_reg(struct device *dev, unsigned int reg)
  173. {
  174. /* Enable caching in interrupt registers */
  175. switch (reg) {
  176. case RC5T583_INT_EN_SYS1:
  177. case RC5T583_INT_EN_SYS2:
  178. case RC5T583_INT_EN_DCDC:
  179. case RC5T583_INT_EN_RTC:
  180. case RC5T583_INT_EN_ADC1:
  181. case RC5T583_INT_EN_ADC2:
  182. case RC5T583_INT_EN_ADC3:
  183. case RC5T583_GPIO_GPEDGE1:
  184. case RC5T583_GPIO_GPEDGE2:
  185. case RC5T583_GPIO_EN_INT:
  186. return false;
  187. case RC5T583_GPIO_MON_IOIN:
  188. /* This is gpio input register */
  189. return true;
  190. default:
  191. /* Enable caching in gpio registers */
  192. if ((reg >= RC5T583_GPIO_IOSEL) &&
  193. (reg <= RC5T583_GPIO_GPOFUNC))
  194. return false;
  195. /* Enable caching in sleep seq registers */
  196. if ((reg >= RC5T583_SLPSEQ1) && (reg <= RC5T583_SLPSEQ11))
  197. return false;
  198. /* Enable caching of regulator registers */
  199. if ((reg >= RC5T583_REG_DC0CTL) && (reg <= RC5T583_REG_SR3CTL))
  200. return false;
  201. if ((reg >= RC5T583_REG_LDOEN1) &&
  202. (reg <= RC5T583_REG_LDO9DAC_DS))
  203. return false;
  204. break;
  205. }
  206. return true;
  207. }
  208. static const struct regmap_config rc5t583_regmap_config = {
  209. .reg_bits = 8,
  210. .val_bits = 8,
  211. .volatile_reg = volatile_reg,
  212. .max_register = RC5T583_MAX_REGS,
  213. .num_reg_defaults_raw = RC5T583_MAX_REGS,
  214. .cache_type = REGCACHE_RBTREE,
  215. };
  216. static int __devinit rc5t583_i2c_probe(struct i2c_client *i2c,
  217. const struct i2c_device_id *id)
  218. {
  219. struct rc5t583 *rc5t583;
  220. struct rc5t583_platform_data *pdata = i2c->dev.platform_data;
  221. int ret;
  222. bool irq_init_success = false;
  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 = 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. goto err_irq_init;
  243. if (i2c->irq) {
  244. ret = rc5t583_irq_init(rc5t583, i2c->irq, pdata->irq_base);
  245. /* Still continue with waring if irq init fails */
  246. if (ret)
  247. dev_warn(&i2c->dev, "IRQ init failed: %d\n", ret);
  248. else
  249. irq_init_success = true;
  250. }
  251. ret = mfd_add_devices(rc5t583->dev, -1, rc5t583_subdevs,
  252. ARRAY_SIZE(rc5t583_subdevs), NULL, 0);
  253. if (ret) {
  254. dev_err(&i2c->dev, "add mfd devices failed: %d\n", ret);
  255. goto err_add_devs;
  256. }
  257. return 0;
  258. err_add_devs:
  259. if (irq_init_success)
  260. rc5t583_irq_exit(rc5t583);
  261. err_irq_init:
  262. regmap_exit(rc5t583->regmap);
  263. return ret;
  264. }
  265. static int __devexit rc5t583_i2c_remove(struct i2c_client *i2c)
  266. {
  267. struct rc5t583 *rc5t583 = i2c_get_clientdata(i2c);
  268. mfd_remove_devices(rc5t583->dev);
  269. rc5t583_irq_exit(rc5t583);
  270. regmap_exit(rc5t583->regmap);
  271. return 0;
  272. }
  273. static const struct i2c_device_id rc5t583_i2c_id[] = {
  274. {.name = "rc5t583", .driver_data = 0},
  275. {}
  276. };
  277. MODULE_DEVICE_TABLE(i2c, rc5t583_i2c_id);
  278. static struct i2c_driver rc5t583_i2c_driver = {
  279. .driver = {
  280. .name = "rc5t583",
  281. .owner = THIS_MODULE,
  282. },
  283. .probe = rc5t583_i2c_probe,
  284. .remove = __devexit_p(rc5t583_i2c_remove),
  285. .id_table = rc5t583_i2c_id,
  286. };
  287. static int __init rc5t583_i2c_init(void)
  288. {
  289. return i2c_add_driver(&rc5t583_i2c_driver);
  290. }
  291. subsys_initcall(rc5t583_i2c_init);
  292. static void __exit rc5t583_i2c_exit(void)
  293. {
  294. i2c_del_driver(&rc5t583_i2c_driver);
  295. }
  296. module_exit(rc5t583_i2c_exit);
  297. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  298. MODULE_DESCRIPTION("RICOH RC5T583 power management system device driver");
  299. MODULE_LICENSE("GPL v2");