retu-mfd.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Retu 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 */
  35. /* Interrupt sources */
  36. #define RETU_INT_PWR 0 /* Power button */
  37. struct retu_dev {
  38. struct regmap *regmap;
  39. struct device *dev;
  40. struct mutex mutex;
  41. struct regmap_irq_chip_data *irq_data;
  42. };
  43. static struct resource retu_pwrbutton_res[] = {
  44. {
  45. .name = "retu-pwrbutton",
  46. .start = RETU_INT_PWR,
  47. .end = RETU_INT_PWR,
  48. .flags = IORESOURCE_IRQ,
  49. },
  50. };
  51. static struct mfd_cell retu_devs[] = {
  52. {
  53. .name = "retu-wdt"
  54. },
  55. {
  56. .name = "retu-pwrbutton",
  57. .resources = retu_pwrbutton_res,
  58. .num_resources = ARRAY_SIZE(retu_pwrbutton_res),
  59. }
  60. };
  61. static struct regmap_irq retu_irqs[] = {
  62. [RETU_INT_PWR] = {
  63. .mask = 1 << RETU_INT_PWR,
  64. }
  65. };
  66. static struct regmap_irq_chip retu_irq_chip = {
  67. .name = "RETU",
  68. .irqs = retu_irqs,
  69. .num_irqs = ARRAY_SIZE(retu_irqs),
  70. .num_regs = 1,
  71. .status_base = RETU_REG_IDR,
  72. .mask_base = RETU_REG_IMR,
  73. .ack_base = RETU_REG_IDR,
  74. };
  75. /* Retu device registered for the power off. */
  76. static struct retu_dev *retu_pm_power_off;
  77. int retu_read(struct retu_dev *rdev, u8 reg)
  78. {
  79. int ret;
  80. int value;
  81. mutex_lock(&rdev->mutex);
  82. ret = regmap_read(rdev->regmap, reg, &value);
  83. mutex_unlock(&rdev->mutex);
  84. return ret ? ret : value;
  85. }
  86. EXPORT_SYMBOL_GPL(retu_read);
  87. int retu_write(struct retu_dev *rdev, u8 reg, u16 data)
  88. {
  89. int ret;
  90. mutex_lock(&rdev->mutex);
  91. ret = regmap_write(rdev->regmap, reg, data);
  92. mutex_unlock(&rdev->mutex);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(retu_write);
  96. static void retu_power_off(void)
  97. {
  98. struct retu_dev *rdev = retu_pm_power_off;
  99. int reg;
  100. mutex_lock(&retu_pm_power_off->mutex);
  101. /* Ignore power button state */
  102. regmap_read(rdev->regmap, RETU_REG_CC1, &reg);
  103. regmap_write(rdev->regmap, RETU_REG_CC1, reg | 2);
  104. /* Expire watchdog immediately */
  105. regmap_write(rdev->regmap, RETU_REG_WATCHDOG, 0);
  106. /* Wait for poweroff */
  107. for (;;)
  108. cpu_relax();
  109. mutex_unlock(&retu_pm_power_off->mutex);
  110. }
  111. static int retu_regmap_read(void *context, const void *reg, size_t reg_size,
  112. void *val, size_t val_size)
  113. {
  114. int ret;
  115. struct device *dev = context;
  116. struct i2c_client *i2c = to_i2c_client(dev);
  117. BUG_ON(reg_size != 1 || val_size != 2);
  118. ret = i2c_smbus_read_word_data(i2c, *(u8 const *)reg);
  119. if (ret < 0)
  120. return ret;
  121. *(u16 *)val = ret;
  122. return 0;
  123. }
  124. static int retu_regmap_write(void *context, const void *data, size_t count)
  125. {
  126. u8 reg;
  127. u16 val;
  128. struct device *dev = context;
  129. struct i2c_client *i2c = to_i2c_client(dev);
  130. BUG_ON(count != sizeof(reg) + sizeof(val));
  131. memcpy(&reg, data, sizeof(reg));
  132. memcpy(&val, data + sizeof(reg), sizeof(val));
  133. return i2c_smbus_write_word_data(i2c, reg, val);
  134. }
  135. static struct regmap_bus retu_bus = {
  136. .read = retu_regmap_read,
  137. .write = retu_regmap_write,
  138. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  139. };
  140. static struct regmap_config retu_config = {
  141. .reg_bits = 8,
  142. .val_bits = 16,
  143. };
  144. static int __devinit retu_probe(struct i2c_client *i2c,
  145. const struct i2c_device_id *id)
  146. {
  147. struct retu_dev *rdev;
  148. int ret;
  149. rdev = devm_kzalloc(&i2c->dev, sizeof(*rdev), GFP_KERNEL);
  150. if (rdev == NULL)
  151. return -ENOMEM;
  152. i2c_set_clientdata(i2c, rdev);
  153. rdev->dev = &i2c->dev;
  154. mutex_init(&rdev->mutex);
  155. rdev->regmap = devm_regmap_init(&i2c->dev, &retu_bus, &i2c->dev,
  156. &retu_config);
  157. if (IS_ERR(rdev->regmap))
  158. return PTR_ERR(rdev->regmap);
  159. ret = retu_read(rdev, RETU_REG_ASICR);
  160. if (ret < 0) {
  161. dev_err(rdev->dev, "could not read Retu revision: %d\n", ret);
  162. return ret;
  163. }
  164. dev_info(rdev->dev, "Retu%s v%d.%d found\n",
  165. (ret & RETU_REG_ASICR_VILMA) ? " & Vilma" : "",
  166. (ret >> 4) & 0x7, ret & 0xf);
  167. /* Mask all RETU interrupts. */
  168. ret = retu_write(rdev, RETU_REG_IMR, 0xffff);
  169. if (ret < 0)
  170. return ret;
  171. ret = regmap_add_irq_chip(rdev->regmap, i2c->irq, IRQF_ONESHOT, -1,
  172. &retu_irq_chip, &rdev->irq_data);
  173. if (ret < 0)
  174. return ret;
  175. ret = mfd_add_devices(rdev->dev, -1, retu_devs, ARRAY_SIZE(retu_devs),
  176. NULL, regmap_irq_chip_get_base(rdev->irq_data),
  177. NULL);
  178. if (ret < 0) {
  179. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  180. return ret;
  181. }
  182. if (!pm_power_off) {
  183. retu_pm_power_off = rdev;
  184. pm_power_off = retu_power_off;
  185. }
  186. return 0;
  187. }
  188. static int __devexit retu_remove(struct i2c_client *i2c)
  189. {
  190. struct retu_dev *rdev = i2c_get_clientdata(i2c);
  191. if (retu_pm_power_off == rdev) {
  192. pm_power_off = NULL;
  193. retu_pm_power_off = NULL;
  194. }
  195. mfd_remove_devices(rdev->dev);
  196. regmap_del_irq_chip(i2c->irq, rdev->irq_data);
  197. return 0;
  198. }
  199. static const struct i2c_device_id retu_id[] = {
  200. { "retu-mfd", 0 },
  201. { }
  202. };
  203. MODULE_DEVICE_TABLE(i2c, retu_id);
  204. static struct i2c_driver retu_driver = {
  205. .driver = {
  206. .name = "retu-mfd",
  207. .owner = THIS_MODULE,
  208. },
  209. .probe = retu_probe,
  210. .remove = retu_remove,
  211. .id_table = retu_id,
  212. };
  213. module_i2c_driver(retu_driver);
  214. MODULE_DESCRIPTION("Retu MFD driver");
  215. MODULE_AUTHOR("Juha Yrjölä");
  216. MODULE_AUTHOR("David Weinehall");
  217. MODULE_AUTHOR("Mikko Ylinen");
  218. MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
  219. MODULE_LICENSE("GPL");