mmp-driver.c 14 KB

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