mmp-driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 <linux/clk.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. struct clk *mipi_clk;
  39. int irq;
  40. };
  41. static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam)
  42. {
  43. return container_of(mcam, struct mmp_camera, mcam);
  44. }
  45. /*
  46. * A silly little infrastructure so we can keep track of our devices.
  47. * Chances are that we will never have more than one of them, but
  48. * the Armada 610 *does* have two controllers...
  49. */
  50. static LIST_HEAD(mmpcam_devices);
  51. static struct mutex mmpcam_devices_lock;
  52. static void mmpcam_add_device(struct mmp_camera *cam)
  53. {
  54. mutex_lock(&mmpcam_devices_lock);
  55. list_add(&cam->devlist, &mmpcam_devices);
  56. mutex_unlock(&mmpcam_devices_lock);
  57. }
  58. static void mmpcam_remove_device(struct mmp_camera *cam)
  59. {
  60. mutex_lock(&mmpcam_devices_lock);
  61. list_del(&cam->devlist);
  62. mutex_unlock(&mmpcam_devices_lock);
  63. }
  64. /*
  65. * Platform dev remove passes us a platform_device, and there's
  66. * no handy unused drvdata to stash a backpointer in. So just
  67. * dig it out of our list.
  68. */
  69. static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev)
  70. {
  71. struct mmp_camera *cam;
  72. mutex_lock(&mmpcam_devices_lock);
  73. list_for_each_entry(cam, &mmpcam_devices, devlist) {
  74. if (cam->pdev == pdev) {
  75. mutex_unlock(&mmpcam_devices_lock);
  76. return cam;
  77. }
  78. }
  79. mutex_unlock(&mmpcam_devices_lock);
  80. return NULL;
  81. }
  82. /*
  83. * Power-related registers; this almost certainly belongs
  84. * somewhere else.
  85. *
  86. * ARMADA 610 register manual, sec 7.2.1, p1842.
  87. */
  88. #define CPU_SUBSYS_PMU_BASE 0xd4282800
  89. #define REG_CCIC_DCGCR 0x28 /* CCIC dyn clock gate ctrl reg */
  90. #define REG_CCIC_CRCR 0x50 /* CCIC clk reset ctrl reg */
  91. /*
  92. * Power control.
  93. */
  94. static void mmpcam_power_up_ctlr(struct mmp_camera *cam)
  95. {
  96. iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR);
  97. iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR);
  98. mdelay(1);
  99. }
  100. static int mmpcam_power_up(struct mcam_camera *mcam)
  101. {
  102. struct mmp_camera *cam = mcam_to_cam(mcam);
  103. struct mmp_camera_platform_data *pdata;
  104. if (mcam->bus_type == V4L2_MBUS_CSI2) {
  105. cam->mipi_clk = devm_clk_get(mcam->dev, "mipi");
  106. if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0))
  107. return PTR_ERR(cam->mipi_clk);
  108. }
  109. /*
  110. * Turn on power and clocks to the controller.
  111. */
  112. mmpcam_power_up_ctlr(cam);
  113. /*
  114. * Provide power to the sensor.
  115. */
  116. mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002);
  117. pdata = cam->pdev->dev.platform_data;
  118. gpio_set_value(pdata->sensor_power_gpio, 1);
  119. mdelay(5);
  120. mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000);
  121. gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */
  122. mdelay(5);
  123. gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */
  124. mdelay(5);
  125. return 0;
  126. }
  127. static void mmpcam_power_down(struct mcam_camera *mcam)
  128. {
  129. struct mmp_camera *cam = mcam_to_cam(mcam);
  130. struct mmp_camera_platform_data *pdata;
  131. /*
  132. * Turn off clocks and set reset lines
  133. */
  134. iowrite32(0, cam->power_regs + REG_CCIC_DCGCR);
  135. iowrite32(0, cam->power_regs + REG_CCIC_CRCR);
  136. /*
  137. * Shut down the sensor.
  138. */
  139. pdata = cam->pdev->dev.platform_data;
  140. gpio_set_value(pdata->sensor_power_gpio, 0);
  141. gpio_set_value(pdata->sensor_reset_gpio, 0);
  142. if (mcam->bus_type == V4L2_MBUS_CSI2 && !IS_ERR(cam->mipi_clk)) {
  143. if (cam->mipi_clk)
  144. devm_clk_put(mcam->dev, cam->mipi_clk);
  145. cam->mipi_clk = NULL;
  146. }
  147. }
  148. /*
  149. * calc the dphy register values
  150. * There are three dphy registers being used.
  151. * dphy[0] - CSI2_DPHY3
  152. * dphy[1] - CSI2_DPHY5
  153. * dphy[2] - CSI2_DPHY6
  154. * CSI2_DPHY3 and CSI2_DPHY6 can be set with a default value
  155. * or be calculated dynamically
  156. */
  157. void mmpcam_calc_dphy(struct mcam_camera *mcam)
  158. {
  159. struct mmp_camera *cam = mcam_to_cam(mcam);
  160. struct mmp_camera_platform_data *pdata = cam->pdev->dev.platform_data;
  161. struct device *dev = &cam->pdev->dev;
  162. unsigned long tx_clk_esc;
  163. /*
  164. * If CSI2_DPHY3 is calculated dynamically,
  165. * pdata->lane_clk should be already set
  166. * either in the board driver statically
  167. * or in the sensor driver dynamically.
  168. */
  169. /*
  170. * dphy[0] - CSI2_DPHY3:
  171. * bit 0 ~ bit 7: HS Term Enable.
  172. * defines the time that the DPHY
  173. * wait before enabling the data
  174. * lane termination after detecting
  175. * that the sensor has driven the data
  176. * lanes to the LP00 bridge state.
  177. * The value is calculated by:
  178. * (Max T(D_TERM_EN)/Period(DDR)) - 1
  179. * bit 8 ~ bit 15: HS_SETTLE
  180. * Time interval during which the HS
  181. * receiver shall ignore any Data Lane
  182. * HS transistions.
  183. * The vaule has been calibrated on
  184. * different boards. It seems to work well.
  185. *
  186. * More detail please refer
  187. * MIPI Alliance Spectification for D-PHY
  188. * document for explanation of HS-SETTLE
  189. * and D-TERM-EN.
  190. */
  191. switch (pdata->dphy3_algo) {
  192. case DPHY3_ALGO_PXA910:
  193. /*
  194. * Calculate CSI2_DPHY3 algo for PXA910
  195. */
  196. pdata->dphy[0] =
  197. (((1 + (pdata->lane_clk * 80) / 1000) & 0xff) << 8)
  198. | (1 + pdata->lane_clk * 35 / 1000);
  199. break;
  200. case DPHY3_ALGO_PXA2128:
  201. /*
  202. * Calculate CSI2_DPHY3 algo for PXA2128
  203. */
  204. pdata->dphy[0] =
  205. (((2 + (pdata->lane_clk * 110) / 1000) & 0xff) << 8)
  206. | (1 + pdata->lane_clk * 35 / 1000);
  207. break;
  208. default:
  209. /*
  210. * Use default CSI2_DPHY3 value for PXA688/PXA988
  211. */
  212. dev_dbg(dev, "camera: use the default CSI2_DPHY3 value\n");
  213. }
  214. /*
  215. * mipi_clk will never be changed, it is a fixed value on MMP
  216. */
  217. if (IS_ERR(cam->mipi_clk))
  218. return;
  219. /* get the escape clk, this is hard coded */
  220. tx_clk_esc = (clk_get_rate(cam->mipi_clk) / 1000000) / 12;
  221. /*
  222. * dphy[2] - CSI2_DPHY6:
  223. * bit 0 ~ bit 7: CK Term Enable
  224. * Time for the Clock Lane receiver to enable the HS line
  225. * termination. The value is calculated similarly with
  226. * HS Term Enable
  227. * bit 8 ~ bit 15: CK Settle
  228. * Time interval during which the HS receiver shall ignore
  229. * any Clock Lane HS transitions.
  230. * The value is calibrated on the boards.
  231. */
  232. pdata->dphy[2] =
  233. ((((534 * tx_clk_esc) / 2000 - 1) & 0xff) << 8)
  234. | (((38 * tx_clk_esc) / 1000 - 1) & 0xff);
  235. dev_dbg(dev, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
  236. pdata->dphy[0], pdata->dphy[1], pdata->dphy[2]);
  237. }
  238. static irqreturn_t mmpcam_irq(int irq, void *data)
  239. {
  240. struct mcam_camera *mcam = data;
  241. unsigned int irqs, handled;
  242. spin_lock(&mcam->dev_lock);
  243. irqs = mcam_reg_read(mcam, REG_IRQSTAT);
  244. handled = mccic_irq(mcam, irqs);
  245. spin_unlock(&mcam->dev_lock);
  246. return IRQ_RETVAL(handled);
  247. }
  248. static int mmpcam_probe(struct platform_device *pdev)
  249. {
  250. struct mmp_camera *cam;
  251. struct mcam_camera *mcam;
  252. struct resource *res;
  253. struct mmp_camera_platform_data *pdata;
  254. int ret;
  255. pdata = pdev->dev.platform_data;
  256. if (!pdata)
  257. return -ENODEV;
  258. cam = kzalloc(sizeof(*cam), GFP_KERNEL);
  259. if (cam == NULL)
  260. return -ENOMEM;
  261. cam->pdev = pdev;
  262. cam->mipi_clk = NULL;
  263. INIT_LIST_HEAD(&cam->devlist);
  264. mcam = &cam->mcam;
  265. mcam->plat_power_up = mmpcam_power_up;
  266. mcam->plat_power_down = mmpcam_power_down;
  267. mcam->calc_dphy = mmpcam_calc_dphy;
  268. mcam->dev = &pdev->dev;
  269. mcam->use_smbus = 0;
  270. mcam->mclk_min = pdata->mclk_min;
  271. mcam->mclk_src = pdata->mclk_src;
  272. mcam->mclk_div = pdata->mclk_div;
  273. mcam->bus_type = pdata->bus_type;
  274. mcam->dphy = pdata->dphy;
  275. mcam->mipi_enabled = false;
  276. mcam->lane = pdata->lane;
  277. mcam->chip_id = MCAM_ARMADA610;
  278. mcam->buffer_mode = B_DMA_sg;
  279. spin_lock_init(&mcam->dev_lock);
  280. /*
  281. * Get our I/O memory.
  282. */
  283. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  284. if (res == NULL) {
  285. dev_err(&pdev->dev, "no iomem resource!\n");
  286. ret = -ENODEV;
  287. goto out_free;
  288. }
  289. mcam->regs = ioremap(res->start, resource_size(res));
  290. if (mcam->regs == NULL) {
  291. dev_err(&pdev->dev, "MMIO ioremap fail\n");
  292. ret = -ENODEV;
  293. goto out_free;
  294. }
  295. mcam->regs_size = resource_size(res);
  296. /*
  297. * Power/clock memory is elsewhere; get it too. Perhaps this
  298. * should really be managed outside of this driver?
  299. */
  300. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  301. if (res == NULL) {
  302. dev_err(&pdev->dev, "no power resource!\n");
  303. ret = -ENODEV;
  304. goto out_unmap1;
  305. }
  306. cam->power_regs = ioremap(res->start, resource_size(res));
  307. if (cam->power_regs == NULL) {
  308. dev_err(&pdev->dev, "power MMIO ioremap fail\n");
  309. ret = -ENODEV;
  310. goto out_unmap1;
  311. }
  312. /*
  313. * Find the i2c adapter. This assumes, of course, that the
  314. * i2c bus is already up and functioning.
  315. */
  316. mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device);
  317. if (mcam->i2c_adapter == NULL) {
  318. ret = -ENODEV;
  319. dev_err(&pdev->dev, "No i2c adapter\n");
  320. goto out_unmap2;
  321. }
  322. /*
  323. * Sensor GPIO pins.
  324. */
  325. ret = gpio_request(pdata->sensor_power_gpio, "cam-power");
  326. if (ret) {
  327. dev_err(&pdev->dev, "Can't get sensor power gpio %d",
  328. pdata->sensor_power_gpio);
  329. goto out_unmap2;
  330. }
  331. gpio_direction_output(pdata->sensor_power_gpio, 0);
  332. ret = gpio_request(pdata->sensor_reset_gpio, "cam-reset");
  333. if (ret) {
  334. dev_err(&pdev->dev, "Can't get sensor reset gpio %d",
  335. pdata->sensor_reset_gpio);
  336. goto out_gpio;
  337. }
  338. gpio_direction_output(pdata->sensor_reset_gpio, 0);
  339. /*
  340. * Power the device up and hand it off to the core.
  341. */
  342. ret = mmpcam_power_up(mcam);
  343. if (ret)
  344. goto out_gpio2;
  345. ret = mccic_register(mcam);
  346. if (ret)
  347. goto out_pwdn;
  348. /*
  349. * Finally, set up our IRQ now that the core is ready to
  350. * deal with it.
  351. */
  352. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  353. if (res == NULL) {
  354. ret = -ENODEV;
  355. goto out_unregister;
  356. }
  357. cam->irq = res->start;
  358. ret = request_irq(cam->irq, mmpcam_irq, IRQF_SHARED,
  359. "mmp-camera", mcam);
  360. if (ret == 0) {
  361. mmpcam_add_device(cam);
  362. return 0;
  363. }
  364. out_unregister:
  365. mccic_shutdown(mcam);
  366. out_pwdn:
  367. mmpcam_power_down(mcam);
  368. out_gpio2:
  369. gpio_free(pdata->sensor_reset_gpio);
  370. out_gpio:
  371. gpio_free(pdata->sensor_power_gpio);
  372. out_unmap2:
  373. iounmap(cam->power_regs);
  374. out_unmap1:
  375. iounmap(mcam->regs);
  376. out_free:
  377. kfree(cam);
  378. return ret;
  379. }
  380. static int mmpcam_remove(struct mmp_camera *cam)
  381. {
  382. struct mcam_camera *mcam = &cam->mcam;
  383. struct mmp_camera_platform_data *pdata;
  384. mmpcam_remove_device(cam);
  385. free_irq(cam->irq, mcam);
  386. mccic_shutdown(mcam);
  387. mmpcam_power_down(mcam);
  388. pdata = cam->pdev->dev.platform_data;
  389. gpio_free(pdata->sensor_reset_gpio);
  390. gpio_free(pdata->sensor_power_gpio);
  391. iounmap(cam->power_regs);
  392. iounmap(mcam->regs);
  393. kfree(cam);
  394. return 0;
  395. }
  396. static int mmpcam_platform_remove(struct platform_device *pdev)
  397. {
  398. struct mmp_camera *cam = mmpcam_find_device(pdev);
  399. if (cam == NULL)
  400. return -ENODEV;
  401. return mmpcam_remove(cam);
  402. }
  403. /*
  404. * Suspend/resume support.
  405. */
  406. #ifdef CONFIG_PM
  407. static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state)
  408. {
  409. struct mmp_camera *cam = mmpcam_find_device(pdev);
  410. if (state.event != PM_EVENT_SUSPEND)
  411. return 0;
  412. mccic_suspend(&cam->mcam);
  413. return 0;
  414. }
  415. static int mmpcam_resume(struct platform_device *pdev)
  416. {
  417. struct mmp_camera *cam = mmpcam_find_device(pdev);
  418. /*
  419. * Power up unconditionally just in case the core tries to
  420. * touch a register even if nothing was active before; trust
  421. * me, it's better this way.
  422. */
  423. mmpcam_power_up_ctlr(cam);
  424. return mccic_resume(&cam->mcam);
  425. }
  426. #endif
  427. static struct platform_driver mmpcam_driver = {
  428. .probe = mmpcam_probe,
  429. .remove = mmpcam_platform_remove,
  430. #ifdef CONFIG_PM
  431. .suspend = mmpcam_suspend,
  432. .resume = mmpcam_resume,
  433. #endif
  434. .driver = {
  435. .name = "mmp-camera",
  436. .owner = THIS_MODULE
  437. }
  438. };
  439. static int __init mmpcam_init_module(void)
  440. {
  441. mutex_init(&mmpcam_devices_lock);
  442. return platform_driver_register(&mmpcam_driver);
  443. }
  444. static void __exit mmpcam_exit_module(void)
  445. {
  446. platform_driver_unregister(&mmpcam_driver);
  447. /*
  448. * platform_driver_unregister() should have emptied the list
  449. */
  450. if (!list_empty(&mmpcam_devices))
  451. printk(KERN_ERR "mmp_camera leaving devices behind\n");
  452. }
  453. module_init(mmpcam_init_module);
  454. module_exit(mmpcam_exit_module);