mmp-driver.c 13 KB

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