retu-mfd.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Retu/Tahvo MFD driver
  3. *
  4. * Copyright (C) 2004, 2005 Nokia Corporation
  5. *
  6. * Based on code written by Juha Yrjölä, David Weinehall and Mikko Ylinen.
  7. * Rewritten by Aaro Koskinen.
  8. *
  9. * This file is subject to the terms and conditions of the GNU General
  10. * Public License. See the file "COPYING" in the main directory of this
  11. * archive for more details.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/err.h>
  19. #include <linux/i2c.h>
  20. #include <linux/irq.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/mutex.h>
  24. #include <linux/module.h>
  25. #include <linux/regmap.h>
  26. #include <linux/mfd/core.h>
  27. #include <linux/mfd/retu.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/moduleparam.h>
  30. /* Registers */
  31. #define RETU_REG_ASICR 0x00 /* ASIC ID and revision */
  32. #define RETU_REG_ASICR_VILMA (1 << 7) /* Bit indicating Vilma */
  33. #define RETU_REG_IDR 0x01 /* Interrupt ID */
  34. #define RETU_REG_IMR 0x02 /* Interrupt mask (Retu) */
  35. #define TAHVO_REG_IMR 0x03 /* Interrupt mask (Tahvo) */
  36. /* Interrupt sources */
  37. #define RETU_INT_PWR 0 /* Power button */
  38. struct retu_dev {
  39. struct regmap *regmap;
  40. struct device *dev;
  41. struct mutex mutex;
  42. struct regmap_irq_chip_data *irq_data;
  43. };
  44. static struct resource retu_pwrbutton_res[] = {
  45. {
  46. .name = "retu-pwrbutton",
  47. .start = RETU_INT_PWR,
  48. .end = RETU_INT_PWR,
  49. .flags = IORESOURCE_IRQ,
  50. },
  51. };
  52. static struct mfd_cell retu_devs[] = {
  53. {
  54. .name = "retu-wdt"
  55. },
  56. {
  57. .name = "retu-pwrbutton",
  58. .resources = retu_pwrbutton_res,
  59. .num_resources = ARRAY_SIZE(retu_pwrbutton_res),
  60. }
  61. };
  62. static struct regmap_irq retu_irqs[] = {
  63. [RETU_INT_PWR] = {
  64. .mask = 1 << RETU_INT_PWR,
  65. }
  66. };
  67. static struct regmap_irq_chip retu_irq_chip = {
  68. .name = "RETU",
  69. .irqs = retu_irqs,
  70. .num_irqs = ARRAY_SIZE(retu_irqs),
  71. .num_regs = 1,
  72. .status_base = RETU_REG_IDR,
  73. .mask_base = RETU_REG_IMR,
  74. .ack_base = RETU_REG_IDR,
  75. };
  76. /* Retu device registered for the power off. */
  77. static struct retu_dev *retu_pm_power_off;
  78. static struct resource tahvo_usb_res[] = {
  79. {
  80. .name = "tahvo-usb",
  81. .start = TAHVO_INT_VBUS,
  82. .end = TAHVO_INT_VBUS,
  83. .flags = IORESOURCE_IRQ,
  84. },
  85. };
  86. static struct mfd_cell tahvo_devs[] = {
  87. {
  88. .name = "tahvo-usb",
  89. .resources = tahvo_usb_res,
  90. .num_resources = ARRAY_SIZE(tahvo_usb_res),
  91. },
  92. };
  93. static struct regmap_irq tahvo_irqs[] = {
  94. [TAHVO_INT_VBUS] = {
  95. .mask = 1 << TAHVO_INT_VBUS,
  96. }
  97. };
  98. static struct regmap_irq_chip tahvo_irq_chip = {
  99. .name = "TAHVO",
  100. .irqs = tahvo_irqs,
  101. .num_irqs = ARRAY_SIZE(tahvo_irqs),
  102. .num_regs = 1,
  103. .status_base = RETU_REG_IDR,
  104. .mask_base = TAHVO_REG_IMR,
  105. .ack_base = RETU_REG_IDR,
  106. };
  107. static const struct retu_data {
  108. char *chip_name;
  109. char *companion_name;
  110. struct regmap_irq_chip *irq_chip;
  111. struct mfd_cell *children;
  112. int nchildren;
  113. } retu_data[] = {
  114. [0] = {
  115. .chip_name = "Retu",
  116. .companion_name = "Vilma",
  117. .irq_chip = &retu_irq_chip,
  118. .children = retu_devs,
  119. .nchildren = ARRAY_SIZE(retu_devs),
  120. },
  121. [1] = {
  122. .chip_name = "Tahvo",
  123. .companion_name = "Betty",
  124. .irq_chip = &tahvo_irq_chip,
  125. .children = tahvo_devs,
  126. .nchildren = ARRAY_SIZE(tahvo_devs),
  127. }
  128. };
  129. int retu_read(struct retu_dev *rdev, u8 reg)
  130. {
  131. int ret;
  132. int value;
  133. mutex_lock(&rdev->mutex);
  134. ret = regmap_read(rdev->regmap, reg, &value);
  135. mutex_unlock(&rdev->mutex);
  136. return ret ? ret : value;
  137. }
  138. EXPORT_SYMBOL_GPL(retu_read);
  139. int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
  140. {
  141. int ret;
  142. mutex_lock(&rdev->mutex);
  143. ret = regmap_write(rdev->regmap, reg, data);
  144. mutex_unlock(&rdev->mutex);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(retu_write);
  148. static void retu_power_off(void)
  149. {
  150. struct retu_dev *rdev = retu_pm_power_off;
  151. int reg;
  152. mutex_lock(&retu_pm_power_off->mutex);
  153. /* Ignore power button state */
  154. regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
  155. regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
  156. /* Expire watchdog immediately */
  157. regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
  158. /* Wait for poweroff */
  159. for (;;)
  160. cpu_relax();
  161. mutex_unlock(&retu_pm_power_off->mutex);
  162. }
  163. static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
  164. void *val, size_t val_size)
  165. {
  166. int ret;
  167. struct device *dev = context;
  168. struct i2c_client *i2c = to_i2c_client(dev);
  169. BUG_ON(reg_size != 1 || val_size != 2);
  170. ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
  171. if (ret < 0)
  172. return ret;
  173. *(u16 *)val = ret;
  174. return 0;
  175. }
  176. static int retu_regmap_write(void *context, const void *data, size_t count)
  177. {
  178. u8 reg;
  179. u16 val;
  180. struct device *dev = context;
  181. struct i2c_client *i2c = to_i2c_client(dev);
  182. BUG_ON(count != sizeof(reg) + sizeof(val));
  183. memcpy(&reg, data, sizeof(reg));
  184. memcpy(&val, data + sizeof(reg), sizeof(val));
  185. return i2c_smbus_write_word_data(i2c, reg, val);
  186. }
  187. static struct regmap_bus retu_bus = {
  188. .read = retu_regmap_read,
  189. .write = retu_regmap_write,
  190. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  191. };
  192. static struct regmap_config retu_config = {
  193. .reg_bits = 8,
  194. .val_bits = 16,
  195. };
  196. static int retu_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
  197. {
  198. struct retu_data const *rdat;
  199. struct retu_dev *rdev;
  200. int ret;
  201. if (i2c->addr > ARRAY_SIZE(retu_data))
  202. return -ENODEV;
  203. rdat = &retu_data[i2c->addr - 1];
  204. rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
  205. if (rdev == NULL)
  206. return -ENOMEM;
  207. i2c_set_clientdata(i2c, rdev);
  208. rdev->dev = &i2c->dev;
  209. mutex_init(&rdev->mutex);
  210. rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
  211. &retu_config);
  212. if (IS_ERR(rdev->regmap))
  213. return PTR_ERR(rdev->regmap);
  214. ret = retu_read(rdev, RETU_REG_ASICR);
  215. if (ret < 0) {
  216. dev_err(rdev->dev, "could not read %s revision: %d\n",
  217. rdat->chip_name, ret);
  218. return ret;
  219. }
  220. dev_info(rdev->dev, "%s%s%s v%d.%d found\n", rdat->chip_name,
  221. (ret & RETU_REG_ASICR_VILMA) ? " & " : "",
  222. (ret & RETU_REG_ASICR_VILMA) ? rdat->companion_name : "",
  223. (ret >> 4) & 0x7, ret & 0xf);
  224. /* Mask all interrupts. */
  225. ret = retu_write(rdev, rdat->irq_chip->mask_base, 0xffff);
  226. if (ret < 0)
  227. return ret;
  228. ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
  229. rdat->irq_chip, &rdev->irq_data);
  230. if (ret < 0)
  231. return ret;
  232. ret = mfd_add_devices(rdev->dev, -1, rdat->children, rdat->nchildren,
  233. NULL, regmap_irq_chip_get_base(rdev->irq_data),
  234. NULL);
  235. if (ret < 0) {
  236. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  237. return ret;
  238. }
  239. if (i2c->addr == 1 && !pm_power_off) {
  240. retu_pm_power_off = rdev;
  241. pm_power_off = retu_power_off;
  242. }
  243. return 0;
  244. }
  245. static int retu_remove(struct i2c_client *i2c)
  246. {
  247. struct retu_dev *rdev = i2c_get_clientdata(i2c);
  248. if (retu_pm_power_off == rdev) {
  249. pm_power_off = NULL;
  250. retu_pm_power_off = NULL;
  251. }
  252. mfd_remove_devices(rdev->dev);
  253. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  254. return 0;
  255. }
  256. static const struct i2c_device_id retu_id[] = {
  257. { "retu-mfd", 0 },
  258. { "tahvo-mfd", 0 },
  259. { }
  260. };
  261. MODULE_DEVICE_TABLE(i2c, retu_id);
  262. static struct i2c_driver retu_driver = {
  263. .driver = {
  264. .name = "retu-mfd",
  265. .owner = THIS_MODULE,
  266. },
  267. .probe = retu_probe,
  268. .remove = retu_remove,
  269. .id_table = retu_id,
  270. };
  271. module_i2c_driver(retu_driver);
  272. MODULE_DESCRIPTION("Retu MFD driver");
  273. MODULE_AUTHOR("Juha Yrjölä");
  274. MODULE_AUTHOR("David Weinehall");
  275. MODULE_AUTHOR("Mikko Ylinen");
  276. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
  277. MODULE_LICENSE("GPL");