htc-pasic3.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Core driver for HTC PASIC3 LED/DS1WM chip.
  3. *
  4. * Copyright (C) 2006 Philipp Zabel <philipp.zabel@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/ds1wm.h>
  14. #include <linux/gpio.h>
  15. #include <linux/io.h>
  16. #include <linux/irq.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/mfd/htc-pasic3.h>
  19. struct pasic3_data {
  20. void __iomem *mapping;
  21. unsigned int bus_shift;
  22. struct platform_device *ds1wm_pdev;
  23. struct platform_device *led_pdev;
  24. };
  25. #define REG_ADDR 5
  26. #define REG_DATA 6
  27. #define READ_MODE 0x80
  28. /*
  29. * write to a secondary register on the PASIC3
  30. */
  31. void pasic3_write_register(struct device *dev, u32 reg, u8 val)
  32. {
  33. struct pasic3_data *asic = dev->driver_data;
  34. int bus_shift = asic->bus_shift;
  35. void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
  36. void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
  37. __raw_writeb(~READ_MODE & reg, addr);
  38. __raw_writeb(val, data);
  39. }
  40. EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
  41. /*
  42. * read from a secondary register on the PASIC3
  43. */
  44. u8 pasic3_read_register(struct device *dev, u32 reg)
  45. {
  46. struct pasic3_data *asic = dev->driver_data;
  47. int bus_shift = asic->bus_shift;
  48. void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
  49. void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
  50. __raw_writeb(READ_MODE | reg, addr);
  51. return __raw_readb(data);
  52. }
  53. EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
  54. /*
  55. * LEDs
  56. */
  57. static int led_device_add(struct device *pasic3_dev,
  58. const struct pasic3_leds_machinfo *pdata)
  59. {
  60. struct pasic3_data *asic = pasic3_dev->driver_data;
  61. struct platform_device *pdev;
  62. int ret;
  63. pdev = platform_device_alloc("pasic3-led", -1);
  64. if (!pdev) {
  65. dev_dbg(pasic3_dev, "failed to allocate LED platform device\n");
  66. return -ENOMEM;
  67. }
  68. ret = platform_device_add_data(pdev, pdata,
  69. sizeof(struct pasic3_leds_machinfo));
  70. if (ret < 0) {
  71. dev_dbg(pasic3_dev, "failed to add LED platform data\n");
  72. goto exit_pdev_put;
  73. }
  74. pdev->dev.parent = pasic3_dev;
  75. ret = platform_device_add(pdev);
  76. if (ret < 0) {
  77. dev_dbg(pasic3_dev, "failed to add LED platform device\n");
  78. goto exit_pdev_put;
  79. }
  80. asic->led_pdev = pdev;
  81. return 0;
  82. exit_pdev_put:
  83. platform_device_put(pdev);
  84. return ret;
  85. }
  86. /*
  87. * DS1WM
  88. */
  89. static void ds1wm_enable(struct platform_device *pdev)
  90. {
  91. struct device *dev = pdev->dev.parent;
  92. int c;
  93. c = pasic3_read_register(dev, 0x28);
  94. pasic3_write_register(dev, 0x28, c & 0x7f);
  95. dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
  96. }
  97. static void ds1wm_disable(struct platform_device *pdev)
  98. {
  99. struct device *dev = pdev->dev.parent;
  100. int c;
  101. c = pasic3_read_register(dev, 0x28);
  102. pasic3_write_register(dev, 0x28, c | 0x80);
  103. dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
  104. }
  105. static struct ds1wm_platform_data ds1wm_pdata = {
  106. .bus_shift = 2,
  107. .enable = ds1wm_enable,
  108. .disable = ds1wm_disable,
  109. };
  110. static int ds1wm_device_add(struct platform_device *pasic3_pdev, int bus_shift)
  111. {
  112. struct device *pasic3_dev = &pasic3_pdev->dev;
  113. struct pasic3_data *asic = pasic3_dev->driver_data;
  114. struct platform_device *pdev;
  115. int ret;
  116. pdev = platform_device_alloc("ds1wm", -1);
  117. if (!pdev) {
  118. dev_dbg(pasic3_dev, "failed to allocate DS1WM platform device\n");
  119. return -ENOMEM;
  120. }
  121. ret = platform_device_add_resources(pdev, pasic3_pdev->resource,
  122. pasic3_pdev->num_resources);
  123. if (ret < 0) {
  124. dev_dbg(pasic3_dev, "failed to add DS1WM resources\n");
  125. goto exit_pdev_put;
  126. }
  127. ds1wm_pdata.bus_shift = asic->bus_shift;
  128. ret = platform_device_add_data(pdev, &ds1wm_pdata,
  129. sizeof(struct ds1wm_platform_data));
  130. if (ret < 0) {
  131. dev_dbg(pasic3_dev, "failed to add DS1WM platform data\n");
  132. goto exit_pdev_put;
  133. }
  134. pdev->dev.parent = pasic3_dev;
  135. ret = platform_device_add(pdev);
  136. if (ret < 0) {
  137. dev_dbg(pasic3_dev, "failed to add DS1WM platform device\n");
  138. goto exit_pdev_put;
  139. }
  140. asic->ds1wm_pdev = pdev;
  141. return 0;
  142. exit_pdev_put:
  143. platform_device_put(pdev);
  144. return ret;
  145. }
  146. static int __init pasic3_probe(struct platform_device *pdev)
  147. {
  148. struct pasic3_platform_data *pdata = pdev->dev.platform_data;
  149. struct device *dev = &pdev->dev;
  150. struct pasic3_data *asic;
  151. struct resource *r;
  152. int ret;
  153. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  154. if (!r)
  155. return -ENXIO;
  156. if (!request_mem_region(r->start, r->end - r->start + 1, "pasic3"))
  157. return -EBUSY;
  158. asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL);
  159. if (!asic)
  160. return -ENOMEM;
  161. platform_set_drvdata(pdev, asic);
  162. if (pdata && pdata->bus_shift)
  163. asic->bus_shift = pdata->bus_shift;
  164. else
  165. asic->bus_shift = 2;
  166. asic->mapping = ioremap(r->start, r->end - r->start + 1);
  167. if (!asic->mapping) {
  168. dev_err(dev, "couldn't ioremap PASIC3\n");
  169. kfree(asic);
  170. return -ENOMEM;
  171. }
  172. ret = ds1wm_device_add(pdev, asic->bus_shift);
  173. if (ret < 0)
  174. dev_warn(dev, "failed to register DS1WM\n");
  175. if (pdata->led_pdata) {
  176. ret = led_device_add(dev, pdata->led_pdata);
  177. if (ret < 0)
  178. dev_warn(dev, "failed to register LED device\n");
  179. }
  180. return 0;
  181. }
  182. static int pasic3_remove(struct platform_device *pdev)
  183. {
  184. struct pasic3_data *asic = platform_get_drvdata(pdev);
  185. struct resource *r;
  186. if (asic->led_pdev)
  187. platform_device_unregister(asic->led_pdev);
  188. if (asic->ds1wm_pdev)
  189. platform_device_unregister(asic->ds1wm_pdev);
  190. iounmap(asic->mapping);
  191. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  192. release_mem_region(r->start, r->end - r->start + 1);
  193. kfree(asic);
  194. return 0;
  195. }
  196. MODULE_ALIAS("platform:pasic3");
  197. static struct platform_driver pasic3_driver = {
  198. .driver = {
  199. .name = "pasic3",
  200. },
  201. .remove = pasic3_remove,
  202. };
  203. static int __init pasic3_base_init(void)
  204. {
  205. return platform_driver_probe(&pasic3_driver, pasic3_probe);
  206. }
  207. static void __exit pasic3_base_exit(void)
  208. {
  209. platform_driver_unregister(&pasic3_driver);
  210. }
  211. module_init(pasic3_base_init);
  212. module_exit(pasic3_base_exit);
  213. MODULE_AUTHOR("Philipp Zabel <philipp.zabel@gmail.com>");
  214. MODULE_DESCRIPTION("Core driver for HTC PASIC3");
  215. MODULE_LICENSE("GPL");