mmp-driver.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Support for the camera device found on Marvell MMP processors; known
  3. * to work with the Armada 610 as used in the OLPC 1.75 system.
  4. *
  5. * Copyright 2011 Jonathan Corbet <corbet@lwn.net>
  6. *
  7. * This file may be distributed under the terms of the GNU General
  8. * Public License, version 2.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-gpio.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/slab.h>
  18. #include <linux/videodev2.h>
  19. #include <media/v4l2-device.h>
  20. #include <media/v4l2-chip-ident.h>
  21. #include <media/mmp-camera.h>
  22. #include <linux/device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/gpio.h>
  25. #include <linux/io.h>
  26. #include <linux/delay.h>
  27. #include <linux/list.h>
  28. #include "mcam-core.h"
  29. MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
  30. MODULE_LICENSE("GPL");
  31. struct mmp_camera {
  32. void *power_regs;
  33. struct platform_device *pdev;
  34. struct mcam_camera mcam;
  35. struct list_head devlist;
  36. int irq;
  37. };
  38. static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
  39. {
  40. return container_of(mcam, struct mmp_camera, mcam);
  41. }
  42. /*
  43. * A silly little infrastructure so we can keep track of our devices.
  44. * Chances are that we will never have more than one of them, but
  45. * the Armada 610 *does* have two controllers...
  46. */
  47. static LIST_HEAD(mmpcam_devices);
  48. static struct mutex mmpcam_devices_lock;
  49. static void mmpcam_add_device(struct mmp_camera *cam)
  50. {
  51. mutex_lock(&mmpcam_devices_lock);
  52. list_add(&cam->devlist, &mmpcam_devices);
  53. mutex_unlock(&mmpcam_devices_lock);
  54. }
  55. static void mmpcam_remove_device(struct mmp_camera *cam)
  56. {
  57. mutex_lock(&mmpcam_devices_lock);
  58. list_del(&cam->devlist);
  59. mutex_unlock(&mmpcam_devices_lock);
  60. }
  61. /*
  62. * Platform dev remove passes us a platform_device, and there's
  63. * no handy unused drvdata to stash a backpointer in. So just
  64. * dig it out of our list.
  65. */
  66. static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
  67. {
  68. struct mmp_camera *cam;
  69. mutex_lock(&mmpcam_devices_lock);
  70. list_for_each_entry(cam, &mmpcam_devices, devlist) {
  71. if (cam->pdev == pdev) {
  72. mutex_unlock(&mmpcam_devices_lock);
  73. return cam;
  74. }
  75. }
  76. mutex_unlock(&mmpcam_devices_lock);
  77. return NULL;
  78. }
  79. /*
  80. * Power-related registers; this almost certainly belongs
  81. * somewhere else.
  82. *
  83. * ARMADA 610 register manual, sec 7.2.1, p1842.
  84. */
  85. #define CPU_SUBSYS_PMU_BASE 0xd4282800
  86. #define REG_CCIC_DCGCR 0x28 /* CCIC dyn clock gate ctrl reg */
  87. #define REG_CCIC_CRCR 0x50 /* CCIC clk reset ctrl reg */
  88. /*
  89. * Power control.
  90. */
  91. static void mmpcam_power_up(struct mcam_camera *mcam)
  92. {
  93. struct mmp_camera *cam = mcam_to_cam(mcam);
  94. struct mmp_camera_platform_data *pdata;
  95. /*
  96. * Turn on power and clocks to the controller.
  97. */
  98. iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
  99. iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
  100. mdelay(1);
  101. /*
  102. * Provide power to the sensor.
  103. */
  104. mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
  105. pdata = cam->pdev->dev.platform_data;
  106. gpio_set_value(pdata->sensor_power_gpio, 1);
  107. mdelay(5);
  108. mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
  109. gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
  110. mdelay(5);
  111. gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
  112. mdelay(5);
  113. }
  114. static void mmpcam_power_down(struct mcam_camera *mcam)
  115. {
  116. struct mmp_camera *cam = mcam_to_cam(mcam);
  117. struct mmp_camera_platform_data *pdata;
  118. /*
  119. * Turn off clocks and set reset lines
  120. */
  121. iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
  122. iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
  123. /*
  124. * Shut down the sensor.
  125. */
  126. pdata = cam->pdev->dev.platform_data;
  127. gpio_set_value(pdata->sensor_power_gpio, 0);
  128. gpio_set_value(pdata->sensor_reset_gpio, 0);
  129. }
  130. static irqreturn_t mmpcam_irq(int irq, void *data)
  131. {
  132. struct mcam_camera *mcam = data;
  133. unsigned int irqs, handled;
  134. spin_lock(&mcam->dev_lock);
  135. irqs = mcam_reg_read(mcam, REG_IRQSTAT);
  136. handled = mccic_irq(mcam, irqs);
  137. spin_unlock(&mcam->dev_lock);
  138. return IRQ_RETVAL(handled);
  139. }
  140. static int mmpcam_probe(struct platform_device *pdev)
  141. {
  142. struct mmp_camera *cam;
  143. struct mcam_camera *mcam;
  144. struct resource *res;
  145. struct mmp_camera_platform_data *pdata;
  146. int ret;
  147. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  148. if (cam == NULL)
  149. return -ENOMEM;
  150. cam->pdev = pdev;
  151. INIT_LIST_HEAD(&cam->devlist);
  152. mcam = &cam->mcam;
  153. mcam->platform = MHP_Armada610;
  154. mcam->plat_power_up = mmpcam_power_up;
  155. mcam->plat_power_down = mmpcam_power_down;
  156. mcam->dev = &pdev->dev;
  157. mcam->use_smbus = 0;
  158. mcam->chip_id = V4L2_IDENT_ARMADA610;
  159. mcam->buffer_mode = B_DMA_sg;
  160. spin_lock_init(&mcam->dev_lock);
  161. /*
  162. * Get our I/O memory.
  163. */
  164. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  165. if (res == NULL) {
  166. dev_err(&pdev->dev, "no iomem resource!\n");
  167. ret = -ENODEV;
  168. goto out_free;
  169. }
  170. mcam->regs = ioremap(res->start, resource_size(res));
  171. if (mcam->regs == NULL) {
  172. dev_err(&pdev->dev, "MMIO ioremap fail\n");
  173. ret = -ENODEV;
  174. goto out_free;
  175. }
  176. /*
  177. * Power/clock memory is elsewhere; get it too. Perhaps this
  178. * should really be managed outside of this driver?
  179. */
  180. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  181. if (res == NULL) {
  182. dev_err(&pdev->dev, "no power resource!\n");
  183. ret = -ENODEV;
  184. goto out_unmap1;
  185. }
  186. cam->power_regs = ioremap(res->start, resource_size(res));
  187. if (cam->power_regs == NULL) {
  188. dev_err(&pdev->dev, "power MMIO ioremap fail\n");
  189. ret = -ENODEV;
  190. goto out_unmap1;
  191. }
  192. /*
  193. * Find the i2c adapter. This assumes, of course, that the
  194. * i2c bus is already up and functioning.
  195. */
  196. pdata = pdev->dev.platform_data;
  197. mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
  198. if (mcam->i2c_adapter == NULL) {
  199. ret = -ENODEV;
  200. dev_err(&pdev->dev, "No i2c adapter\n");
  201. goto out_unmap2;
  202. }
  203. /*
  204. * Sensor GPIO pins.
  205. */
  206. ret = gpio_request(pdata->sensor_power_gpio, "cam-power");
  207. if (ret) {
  208. dev_err(&pdev->dev, "Can't get sensor power gpio %d",
  209. pdata->sensor_power_gpio);
  210. goto out_unmap2;
  211. }
  212. gpio_direction_output(pdata->sensor_power_gpio, 0);
  213. ret = gpio_request(pdata->sensor_reset_gpio, "cam-reset");
  214. if (ret) {
  215. dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
  216. pdata->sensor_reset_gpio);
  217. goto out_gpio;
  218. }
  219. gpio_direction_output(pdata->sensor_reset_gpio, 0);
  220. /*
  221. * Power the device up and hand it off to the core.
  222. */
  223. mmpcam_power_up(mcam);
  224. ret = mccic_register(mcam);
  225. if (ret)
  226. goto out_gpio2;
  227. /*
  228. * Finally, set up our IRQ now that the core is ready to
  229. * deal with it.
  230. */
  231. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  232. if (res == NULL) {
  233. ret = -ENODEV;
  234. goto out_unregister;
  235. }
  236. cam->irq = res->start;
  237. ret = request_irq(cam->irq, mmpcam_irq, IRQF_SHARED,
  238. "mmp-camera", mcam);
  239. if (ret == 0) {
  240. mmpcam_add_device(cam);
  241. return 0;
  242. }
  243. out_unregister:
  244. mccic_shutdown(mcam);
  245. mmpcam_power_down(mcam);
  246. out_gpio2:
  247. gpio_free(pdata->sensor_reset_gpio);
  248. out_gpio:
  249. gpio_free(pdata->sensor_power_gpio);
  250. out_unmap2:
  251. iounmap(cam->power_regs);
  252. out_unmap1:
  253. iounmap(mcam->regs);
  254. out_free:
  255. kfree(cam);
  256. return ret;
  257. }
  258. static int mmpcam_remove(struct mmp_camera *cam)
  259. {
  260. struct mcam_camera *mcam = &cam->mcam;
  261. struct mmp_camera_platform_data *pdata;
  262. mmpcam_remove_device(cam);
  263. free_irq(cam->irq, mcam);
  264. mccic_shutdown(mcam);
  265. mmpcam_power_down(mcam);
  266. pdata = cam->pdev->dev.platform_data;
  267. gpio_free(pdata->sensor_reset_gpio);
  268. gpio_free(pdata->sensor_power_gpio);
  269. iounmap(cam->power_regs);
  270. iounmap(mcam->regs);
  271. kfree(cam);
  272. return 0;
  273. }
  274. static int mmpcam_platform_remove(struct platform_device *pdev)
  275. {
  276. struct mmp_camera *cam = mmpcam_find_device(pdev);
  277. if (cam == NULL)
  278. return -ENODEV;
  279. return mmpcam_remove(cam);
  280. }
  281. static struct platform_driver mmpcam_driver = {
  282. .probe = mmpcam_probe,
  283. .remove = mmpcam_platform_remove,
  284. .driver = {
  285. .name = "mmp-camera",
  286. .owner = THIS_MODULE
  287. }
  288. };
  289. static int __init mmpcam_init_module(void)
  290. {
  291. mutex_init(&mmpcam_devices_lock);
  292. return platform_driver_register(&mmpcam_driver);
  293. }
  294. static void __exit mmpcam_exit_module(void)
  295. {
  296. platform_driver_unregister(&mmpcam_driver);
  297. /*
  298. * platform_driver_unregister() should have emptied the list
  299. */
  300. if (!list_empty(&mmpcam_devices))
  301. printk(KERN_ERR "mmp_camera leaving devices behind\n");
  302. }
  303. module_init(mmpcam_init_module);
  304. module_exit(mmpcam_exit_module);