mmp-driver.c 8.2 KB

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