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. #include <asm/arch/pxa-regs.h>
  20. struct pasic3_data {
  21. void __iomem *mapping;
  22. unsigned int bus_shift;
  23. struct platform_device *ds1wm_pdev;
  24. struct platform_device *led_pdev;
  25. };
  26. #define REG_ADDR 5
  27. #define REG_DATA 6
  28. #define NUM_REGS 7
  29. #define READ_MODE 0x80
  30. /*
  31. * write to a secondary register on the PASIC3
  32. */
  33. void pasic3_write_register(struct device *dev, u32 reg, u8 val)
  34. {
  35. struct pasic3_data *asic = dev->driver_data;
  36. int bus_shift = asic->bus_shift;
  37. void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
  38. void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
  39. __raw_writeb(~READ_MODE & reg, addr);
  40. __raw_writeb(val, data);
  41. }
  42. EXPORT_SYMBOL(pasic3_write_register); /* for leds-pasic3 */
  43. /*
  44. * read from a secondary register on the PASIC3
  45. */
  46. u8 pasic3_read_register(struct device *dev, u32 reg)
  47. {
  48. struct pasic3_data *asic = dev->driver_data;
  49. int bus_shift = asic->bus_shift;
  50. void __iomem *addr = asic->mapping + (REG_ADDR << bus_shift);
  51. void __iomem *data = asic->mapping + (REG_DATA << bus_shift);
  52. __raw_writeb(READ_MODE | reg, addr);
  53. return __raw_readb(data);
  54. }
  55. EXPORT_SYMBOL(pasic3_read_register); /* for leds-pasic3 */
  56. /*
  57. * LEDs
  58. */
  59. static int led_device_add(struct device *pasic3_dev,
  60. const struct pasic3_leds_machinfo *pdata)
  61. {
  62. struct pasic3_data *asic = pasic3_dev->driver_data;
  63. struct platform_device *pdev;
  64. int ret;
  65. pdev = platform_device_alloc("pasic3-led", -1);
  66. if (!pdev) {
  67. dev_dbg(pasic3_dev, "failed to allocate LED platform device\n");
  68. return -ENOMEM;
  69. }
  70. ret = platform_device_add_data(pdev, pdata,
  71. sizeof(struct pasic3_leds_machinfo));
  72. if (ret < 0) {
  73. dev_dbg(pasic3_dev, "failed to add LED platform data\n");
  74. goto exit_pdev_put;
  75. }
  76. pdev->dev.parent = pasic3_dev;
  77. ret = platform_device_add(pdev);
  78. if (ret < 0) {
  79. dev_dbg(pasic3_dev, "failed to add LED platform device\n");
  80. goto exit_pdev_put;
  81. }
  82. asic->led_pdev = pdev;
  83. return 0;
  84. exit_pdev_put:
  85. platform_device_put(pdev);
  86. return ret;
  87. }
  88. /*
  89. * DS1WM
  90. */
  91. static void ds1wm_enable(struct platform_device *pdev)
  92. {
  93. struct device *dev = pdev->dev.parent;
  94. int c;
  95. c = pasic3_read_register(dev, 0x28);
  96. pasic3_write_register(dev, 0x28, c & 0x7f);
  97. dev_dbg(dev, "DS1WM OWM_EN low (active) %02x\n", c & 0x7f);
  98. }
  99. static void ds1wm_disable(struct platform_device *pdev)
  100. {
  101. struct device *dev = pdev->dev.parent;
  102. int c;
  103. c = pasic3_read_register(dev, 0x28);
  104. pasic3_write_register(dev, 0x28, c | 0x80);
  105. dev_dbg(dev, "DS1WM OWM_EN high (inactive) %02x\n", c | 0x80);
  106. }
  107. static struct ds1wm_platform_data ds1wm_pdata = {
  108. .bus_shift = 2,
  109. .enable = ds1wm_enable,
  110. .disable = ds1wm_disable,
  111. };
  112. static int ds1wm_device_add(struct device *pasic3_dev, int bus_shift)
  113. {
  114. struct pasic3_data *asic = pasic3_dev->driver_data;
  115. struct platform_device *pdev;
  116. int ret;
  117. pdev = platform_device_alloc("ds1wm", -1);
  118. if (!pdev) {
  119. dev_dbg(pasic3_dev, "failed to allocate DS1WM platform device\n");
  120. return -ENOMEM;
  121. }
  122. ret = platform_device_add_resources(pdev, pdev->resource,
  123. pdev->num_resources);
  124. if (ret < 0) {
  125. dev_dbg(pasic3_dev, "failed to add DS1WM resources\n");
  126. goto exit_pdev_put;
  127. }
  128. ds1wm_pdata.bus_shift = asic->bus_shift;
  129. ret = platform_device_add_data(pdev, &ds1wm_pdata,
  130. sizeof(struct ds1wm_platform_data));
  131. if (ret < 0) {
  132. dev_dbg(pasic3_dev, "failed to add DS1WM platform data\n");
  133. goto exit_pdev_put;
  134. }
  135. pdev->dev.parent = pasic3_dev;
  136. ret = platform_device_add(pdev);
  137. if (ret < 0) {
  138. dev_dbg(pasic3_dev, "failed to add DS1WM platform device\n");
  139. goto exit_pdev_put;
  140. }
  141. asic->ds1wm_pdev = pdev;
  142. return 0;
  143. exit_pdev_put:
  144. platform_device_put(pdev);
  145. return ret;
  146. }
  147. static int __init pasic3_probe(struct platform_device *pdev)
  148. {
  149. struct pasic3_platform_data *pdata = pdev->dev.platform_data;
  150. struct device *dev = &pdev->dev;
  151. struct pasic3_data *asic;
  152. struct resource *r;
  153. int ret;
  154. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  155. if (!r)
  156. return -ENXIO;
  157. if (!request_mem_region(r->start, r->end - r->start + 1, "pasic3"))
  158. return -EBUSY;
  159. asic = kzalloc(sizeof(struct pasic3_data), GFP_KERNEL);
  160. if (!asic)
  161. return -ENOMEM;
  162. platform_set_drvdata(pdev, asic);
  163. if (pdata && pdata->bus_shift)
  164. asic->bus_shift = pdata->bus_shift;
  165. else
  166. asic->bus_shift = 2;
  167. asic->mapping = ioremap(r->start, r->end - r->start + 1);
  168. if (!asic->mapping) {
  169. dev_err(dev, "couldn't ioremap PASIC3\n");
  170. kfree(asic);
  171. return -ENOMEM;
  172. }
  173. ret = ds1wm_device_add(dev, asic->bus_shift);
  174. if (ret < 0)
  175. dev_warn(dev, "failed to register DS1WM\n");
  176. if (pdata->led_pdata) {
  177. ret = led_device_add(dev, pdata->led_pdata);
  178. if (ret < 0)
  179. dev_warn(dev, "failed to register LED device\n");
  180. }
  181. return 0;
  182. }
  183. static int pasic3_remove(struct platform_device *pdev)
  184. {
  185. struct pasic3_data *asic = platform_get_drvdata(pdev);
  186. struct resource *r;
  187. if (asic->led_pdev)
  188. platform_device_unregister(asic->led_pdev);
  189. if (asic->ds1wm_pdev)
  190. platform_device_unregister(asic->ds1wm_pdev);
  191. iounmap(asic->mapping);
  192. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  193. release_mem_region(r->start, r->end - r->start + 1);
  194. kfree(asic);
  195. return 0;
  196. }
  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");