mmp-driver.c 8.9 KB

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