mmp-driver.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/mmp-camera.h>
  21. #include <linux/device.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/gpio.h>
  24. #include <linux/io.h>
  25. #include <linux/delay.h>
  26. #include <linux/list.h>
  27. #include <linux/pm.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_ctlr(struct mmp_camera *cam)
  93. {
  94. iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
  95. iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
  96. mdelay(1);
  97. }
  98. static void mmpcam_power_up(struct mcam_camera *mcam)
  99. {
  100. struct mmp_camera *cam = mcam_to_cam(mcam);
  101. struct mmp_camera_platform_data *pdata;
  102. /*
  103. * Turn on power and clocks to the controller.
  104. */
  105. mmpcam_power_up_ctlr(cam);
  106. /*
  107. * Provide power to the sensor.
  108. */
  109. mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
  110. pdata = cam->pdev->dev.platform_data;
  111. gpio_set_value(pdata->sensor_power_gpio, 1);
  112. mdelay(5);
  113. mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
  114. gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
  115. mdelay(5);
  116. gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
  117. mdelay(5);
  118. }
  119. static void mmpcam_power_down(struct mcam_camera *mcam)
  120. {
  121. struct mmp_camera *cam = mcam_to_cam(mcam);
  122. struct mmp_camera_platform_data *pdata;
  123. /*
  124. * Turn off clocks and set reset lines
  125. */
  126. iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
  127. iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
  128. /*
  129. * Shut down the sensor.
  130. */
  131. pdata = cam->pdev->dev.platform_data;
  132. gpio_set_value(pdata->sensor_power_gpio, 0);
  133. gpio_set_value(pdata->sensor_reset_gpio, 0);
  134. }
  135. static irqreturn_t mmpcam_irq(int irq, void *data)
  136. {
  137. struct mcam_camera *mcam = data;
  138. unsigned int irqs, handled;
  139. spin_lock(&mcam->dev_lock);
  140. irqs = mcam_reg_read(mcam, REG_IRQSTAT);
  141. handled = mccic_irq(mcam, irqs);
  142. spin_unlock(&mcam->dev_lock);
  143. return IRQ_RETVAL(handled);
  144. }
  145. static int mmpcam_probe(struct platform_device *pdev)
  146. {
  147. struct mmp_camera *cam;
  148. struct mcam_camera *mcam;
  149. struct resource *res;
  150. struct mmp_camera_platform_data *pdata;
  151. int ret;
  152. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  153. if (cam == NULL)
  154. return -ENOMEM;
  155. cam->pdev = pdev;
  156. INIT_LIST_HEAD(&cam->devlist);
  157. mcam = &cam->mcam;
  158. mcam->plat_power_up = mmpcam_power_up;
  159. mcam->plat_power_down = mmpcam_power_down;
  160. mcam->dev = &pdev->dev;
  161. mcam->use_smbus = 0;
  162. mcam->chip_id = MCAM_ARMADA610;
  163. mcam->buffer_mode = B_DMA_sg;
  164. spin_lock_init(&mcam->dev_lock);
  165. /*
  166. * Get our I/O memory.
  167. */
  168. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  169. if (res == NULL) {
  170. dev_err(&pdev->dev, "no iomem resource!\n");
  171. ret = -ENODEV;
  172. goto out_free;
  173. }
  174. mcam->regs = ioremap(res->start, resource_size(res));
  175. if (mcam->regs == NULL) {
  176. dev_err(&pdev->dev, "MMIO ioremap fail\n");
  177. ret = -ENODEV;
  178. goto out_free;
  179. }
  180. /*
  181. * Power/clock memory is elsewhere; get it too. Perhaps this
  182. * should really be managed outside of this driver?
  183. */
  184. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  185. if (res == NULL) {
  186. dev_err(&pdev->dev, "no power resource!\n");
  187. ret = -ENODEV;
  188. goto out_unmap1;
  189. }
  190. cam->power_regs = ioremap(res->start, resource_size(res));
  191. if (cam->power_regs == NULL) {
  192. dev_err(&pdev->dev, "power MMIO ioremap fail\n");
  193. ret = -ENODEV;
  194. goto out_unmap1;
  195. }
  196. /*
  197. * Find the i2c adapter. This assumes, of course, that the
  198. * i2c bus is already up and functioning.
  199. */
  200. pdata = pdev->dev.platform_data;
  201. mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
  202. if (mcam->i2c_adapter == NULL) {
  203. ret = -ENODEV;
  204. dev_err(&pdev->dev, "No i2c adapter\n");
  205. goto out_unmap2;
  206. }
  207. /*
  208. * Sensor GPIO pins.
  209. */
  210. ret = gpio_request(pdata->sensor_power_gpio, "cam-power");
  211. if (ret) {
  212. dev_err(&pdev->dev, "Can't get sensor power gpio %d",
  213. pdata->sensor_power_gpio);
  214. goto out_unmap2;
  215. }
  216. gpio_direction_output(pdata->sensor_power_gpio, 0);
  217. ret = gpio_request(pdata->sensor_reset_gpio, "cam-reset");
  218. if (ret) {
  219. dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
  220. pdata->sensor_reset_gpio);
  221. goto out_gpio;
  222. }
  223. gpio_direction_output(pdata->sensor_reset_gpio, 0);
  224. /*
  225. * Power the device up and hand it off to the core.
  226. */
  227. mmpcam_power_up(mcam);
  228. ret = mccic_register(mcam);
  229. if (ret)
  230. goto out_gpio2;
  231. /*
  232. * Finally, set up our IRQ now that the core is ready to
  233. * deal with it.
  234. */
  235. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  236. if (res == NULL) {
  237. ret = -ENODEV;
  238. goto out_unregister;
  239. }
  240. cam->irq = res->start;
  241. ret = request_irq(cam->irq, mmpcam_irq, IRQF_SHARED,
  242. "mmp-camera", mcam);
  243. if (ret == 0) {
  244. mmpcam_add_device(cam);
  245. return 0;
  246. }
  247. out_unregister:
  248. mccic_shutdown(mcam);
  249. out_gpio2:
  250. mmpcam_power_down(mcam);
  251. gpio_free(pdata->sensor_reset_gpio);
  252. out_gpio:
  253. gpio_free(pdata->sensor_power_gpio);
  254. out_unmap2:
  255. iounmap(cam->power_regs);
  256. out_unmap1:
  257. iounmap(mcam->regs);
  258. out_free:
  259. kfree(cam);
  260. return ret;
  261. }
  262. static int mmpcam_remove(struct mmp_camera *cam)
  263. {
  264. struct mcam_camera *mcam = &cam->mcam;
  265. struct mmp_camera_platform_data *pdata;
  266. mmpcam_remove_device(cam);
  267. free_irq(cam->irq, mcam);
  268. mccic_shutdown(mcam);
  269. mmpcam_power_down(mcam);
  270. pdata = cam->pdev->dev.platform_data;
  271. gpio_free(pdata->sensor_reset_gpio);
  272. gpio_free(pdata->sensor_power_gpio);
  273. iounmap(cam->power_regs);
  274. iounmap(mcam->regs);
  275. kfree(cam);
  276. return 0;
  277. }
  278. static int mmpcam_platform_remove(struct platform_device *pdev)
  279. {
  280. struct mmp_camera *cam = mmpcam_find_device(pdev);
  281. if (cam == NULL)
  282. return -ENODEV;
  283. return mmpcam_remove(cam);
  284. }
  285. /*
  286. * Suspend/resume support.
  287. */
  288. #ifdef CONFIG_PM
  289. static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
  290. {
  291. struct mmp_camera *cam = mmpcam_find_device(pdev);
  292. if (state.event != PM_EVENT_SUSPEND)
  293. return 0;
  294. mccic_suspend(&cam->mcam);
  295. return 0;
  296. }
  297. static int mmpcam_resume(struct platform_device *pdev)
  298. {
  299. struct mmp_camera *cam = mmpcam_find_device(pdev);
  300. /*
  301. * Power up unconditionally just in case the core tries to
  302. * touch a register even if nothing was active before; trust
  303. * me, it's better this way.
  304. */
  305. mmpcam_power_up_ctlr(cam);
  306. return mccic_resume(&cam->mcam);
  307. }
  308. #endif
  309. static struct platform_driver mmpcam_driver = {
  310. .probe = mmpcam_probe,
  311. .remove = mmpcam_platform_remove,
  312. #ifdef CONFIG_PM
  313. .suspend = mmpcam_suspend,
  314. .resume = mmpcam_resume,
  315. #endif
  316. .driver = {
  317. .name = "mmp-camera",
  318. .owner = THIS_MODULE
  319. }
  320. };
  321. static int __init mmpcam_init_module(void)
  322. {
  323. mutex_init(&mmpcam_devices_lock);
  324. return platform_driver_register(&mmpcam_driver);
  325. }
  326. static void __exit mmpcam_exit_module(void)
  327. {
  328. platform_driver_unregister(&mmpcam_driver);
  329. /*
  330. * platform_driver_unregister() should have emptied the list
  331. */
  332. if (!list_empty(&mmpcam_devices))
  333. printk(KERN_ERR "mmp_camera leaving devices behind\n");
  334. }
  335. module_init(mmpcam_init_module);
  336. module_exit(mmpcam_exit_module);