mmp-driver.c 14 KB

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