rk808.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * MFD core driver for Rockchip RK808
  3. *
  4. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  5. *
  6. * Author: Chris Zhong <zyw@rock-chips.com>
  7. * Author: Zhang Qing <zhangqing@rock-chips.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. */
  18. #include <linux/i2c.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/mfd/rk808.h>
  21. #include <linux/mfd/core.h>
  22. #include <linux/module.h>
  23. #include <linux/regmap.h>
  24. struct rk808_reg_data {
  25. int addr;
  26. int mask;
  27. int value;
  28. };
  29. static bool rk808_is_volatile_reg(struct device *dev, unsigned int reg)
  30. {
  31. /*
  32. * Notes:
  33. * - Technically the ROUND_30s bit makes RTC_CTRL_REG volatile, but
  34. * we don't use that feature. It's better to cache.
  35. * - It's unlikely we care that RK808_DEVCTRL_REG is volatile since
  36. * bits are cleared in case when we shutoff anyway, but better safe.
  37. */
  38. switch (reg) {
  39. case RK808_SECONDS_REG ... RK808_WEEKS_REG:
  40. case RK808_RTC_STATUS_REG:
  41. case RK808_VB_MON_REG:
  42. case RK808_THERMAL_REG:
  43. case RK808_DCDC_UV_STS_REG:
  44. case RK808_LDO_UV_STS_REG:
  45. case RK808_DCDC_PG_REG:
  46. case RK808_LDO_PG_REG:
  47. case RK808_DEVCTRL_REG:
  48. case RK808_INT_STS_REG1:
  49. case RK808_INT_STS_REG2:
  50. return true;
  51. }
  52. return false;
  53. }
  54. static const struct regmap_config rk808_regmap_config = {
  55. .reg_bits = 8,
  56. .val_bits = 8,
  57. .max_register = RK808_IO_POL_REG,
  58. .cache_type = REGCACHE_RBTREE,
  59. .volatile_reg = rk808_is_volatile_reg,
  60. };
  61. static struct resource rtc_resources[] = {
  62. {
  63. .start = RK808_IRQ_RTC_ALARM,
  64. .end = RK808_IRQ_RTC_ALARM,
  65. .flags = IORESOURCE_IRQ,
  66. }
  67. };
  68. static const struct mfd_cell rk808s[] = {
  69. { .name = "rk808-clkout", },
  70. { .name = "rk808-regulator", },
  71. {
  72. .name = "rk808-rtc",
  73. .num_resources = ARRAY_SIZE(rtc_resources),
  74. .resources = &rtc_resources[0],
  75. },
  76. };
  77. static const struct rk808_reg_data pre_init_reg[] = {
  78. { RK808_BUCK3_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_150MA },
  79. { RK808_BUCK4_CONFIG_REG, BUCK_ILMIN_MASK, BUCK_ILMIN_200MA },
  80. { RK808_BOOST_CONFIG_REG, BOOST_ILMIN_MASK, BOOST_ILMIN_100MA },
  81. { RK808_BUCK1_CONFIG_REG, BUCK1_RATE_MASK, BUCK_ILMIN_200MA },
  82. { RK808_BUCK2_CONFIG_REG, BUCK2_RATE_MASK, BUCK_ILMIN_200MA },
  83. { RK808_DCDC_UV_ACT_REG, BUCK_UV_ACT_MASK, BUCK_UV_ACT_DISABLE},
  84. { RK808_VB_MON_REG, MASK_ALL, VB_LO_ACT |
  85. VB_LO_SEL_3500MV },
  86. };
  87. static const struct regmap_irq rk808_irqs[] = {
  88. /* INT_STS */
  89. [RK808_IRQ_VOUT_LO] = {
  90. .mask = RK808_IRQ_VOUT_LO_MSK,
  91. .reg_offset = 0,
  92. },
  93. [RK808_IRQ_VB_LO] = {
  94. .mask = RK808_IRQ_VB_LO_MSK,
  95. .reg_offset = 0,
  96. },
  97. [RK808_IRQ_PWRON] = {
  98. .mask = RK808_IRQ_PWRON_MSK,
  99. .reg_offset = 0,
  100. },
  101. [RK808_IRQ_PWRON_LP] = {
  102. .mask = RK808_IRQ_PWRON_LP_MSK,
  103. .reg_offset = 0,
  104. },
  105. [RK808_IRQ_HOTDIE] = {
  106. .mask = RK808_IRQ_HOTDIE_MSK,
  107. .reg_offset = 0,
  108. },
  109. [RK808_IRQ_RTC_ALARM] = {
  110. .mask = RK808_IRQ_RTC_ALARM_MSK,
  111. .reg_offset = 0,
  112. },
  113. [RK808_IRQ_RTC_PERIOD] = {
  114. .mask = RK808_IRQ_RTC_PERIOD_MSK,
  115. .reg_offset = 0,
  116. },
  117. /* INT_STS2 */
  118. [RK808_IRQ_PLUG_IN_INT] = {
  119. .mask = RK808_IRQ_PLUG_IN_INT_MSK,
  120. .reg_offset = 1,
  121. },
  122. [RK808_IRQ_PLUG_OUT_INT] = {
  123. .mask = RK808_IRQ_PLUG_OUT_INT_MSK,
  124. .reg_offset = 1,
  125. },
  126. };
  127. static struct regmap_irq_chip rk808_irq_chip = {
  128. .name = "rk808",
  129. .irqs = rk808_irqs,
  130. .num_irqs = ARRAY_SIZE(rk808_irqs),
  131. .num_regs = 2,
  132. .irq_reg_stride = 2,
  133. .status_base = RK808_INT_STS_REG1,
  134. .mask_base = RK808_INT_STS_MSK_REG1,
  135. .ack_base = RK808_INT_STS_REG1,
  136. .init_ack_masked = true,
  137. };
  138. static struct i2c_client *rk808_i2c_client;
  139. static void rk808_device_shutdown(void)
  140. {
  141. int ret;
  142. struct rk808 *rk808 = i2c_get_clientdata(rk808_i2c_client);
  143. if (!rk808) {
  144. dev_warn(&rk808_i2c_client->dev,
  145. "have no rk808, so do nothing here\n");
  146. return;
  147. }
  148. ret = regmap_update_bits(rk808->regmap,
  149. RK808_DEVCTRL_REG,
  150. DEV_OFF_RST, DEV_OFF_RST);
  151. if (ret)
  152. dev_err(&rk808_i2c_client->dev, "power off error!\n");
  153. }
  154. static int rk808_probe(struct i2c_client *client,
  155. const struct i2c_device_id *id)
  156. {
  157. struct device_node *np = client->dev.of_node;
  158. struct rk808 *rk808;
  159. int pm_off = 0;
  160. int ret;
  161. int i;
  162. if (!client->irq) {
  163. dev_err(&client->dev, "No interrupt support, no core IRQ\n");
  164. return -EINVAL;
  165. }
  166. rk808 = devm_kzalloc(&client->dev, sizeof(*rk808), GFP_KERNEL);
  167. if (!rk808)
  168. return -ENOMEM;
  169. rk808->regmap = devm_regmap_init_i2c(client, &rk808_regmap_config);
  170. if (IS_ERR(rk808->regmap)) {
  171. dev_err(&client->dev, "regmap initialization failed\n");
  172. return PTR_ERR(rk808->regmap);
  173. }
  174. for (i = 0; i < ARRAY_SIZE(pre_init_reg); i++) {
  175. ret = regmap_update_bits(rk808->regmap, pre_init_reg[i].addr,
  176. pre_init_reg[i].mask,
  177. pre_init_reg[i].value);
  178. if (ret) {
  179. dev_err(&client->dev,
  180. "0x%x write err\n", pre_init_reg[i].addr);
  181. return ret;
  182. }
  183. }
  184. ret = regmap_add_irq_chip(rk808->regmap, client->irq,
  185. IRQF_ONESHOT, -1,
  186. &rk808_irq_chip, &rk808->irq_data);
  187. if (ret) {
  188. dev_err(&client->dev, "Failed to add irq_chip %d\n", ret);
  189. return ret;
  190. }
  191. rk808->i2c = client;
  192. i2c_set_clientdata(client, rk808);
  193. ret = mfd_add_devices(&client->dev, -1,
  194. rk808s, ARRAY_SIZE(rk808s),
  195. NULL, 0, regmap_irq_get_domain(rk808->irq_data));
  196. if (ret) {
  197. dev_err(&client->dev, "failed to add MFD devices %d\n", ret);
  198. goto err_irq;
  199. }
  200. pm_off = of_property_read_bool(np,
  201. "rockchip,system-power-controller");
  202. if (pm_off && !pm_power_off) {
  203. rk808_i2c_client = client;
  204. pm_power_off = rk808_device_shutdown;
  205. }
  206. return 0;
  207. err_irq:
  208. regmap_del_irq_chip(client->irq, rk808->irq_data);
  209. return ret;
  210. }
  211. static int rk808_remove(struct i2c_client *client)
  212. {
  213. struct rk808 *rk808 = i2c_get_clientdata(client);
  214. regmap_del_irq_chip(client->irq, rk808->irq_data);
  215. mfd_remove_devices(&client->dev);
  216. pm_power_off = NULL;
  217. return 0;
  218. }
  219. static const struct of_device_id rk808_of_match[] = {
  220. { .compatible = "rockchip,rk808" },
  221. { },
  222. };
  223. MODULE_DEVICE_TABLE(of, rk808_of_match);
  224. static const struct i2c_device_id rk808_ids[] = {
  225. { "rk808" },
  226. { },
  227. };
  228. MODULE_DEVICE_TABLE(i2c, rk808_ids);
  229. static struct i2c_driver rk808_i2c_driver = {
  230. .driver = {
  231. .name = "rk808",
  232. .of_match_table = rk808_of_match,
  233. },
  234. .probe = rk808_probe,
  235. .remove = rk808_remove,
  236. .id_table = rk808_ids,
  237. };
  238. module_i2c_driver(rk808_i2c_driver);
  239. MODULE_LICENSE("GPL");
  240. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  241. MODULE_AUTHOR("Zhang Qing <zhangqing@rock-chips.com>");
  242. MODULE_DESCRIPTION("RK808 PMIC driver");