| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Support for the camera device found on Marvell MMP processors; known |
|---|
| 3 | 4 | * to work with the Armada 610 as used in the OLPC 1.75 system. |
|---|
| 4 | 5 | * |
|---|
| 5 | 6 | * 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. |
|---|
| 7 | + * Copyright 2018 Lubomir Rintel <lkundrak@v3.sk> |
|---|
| 9 | 8 | */ |
|---|
| 10 | 9 | |
|---|
| 11 | 10 | #include <linux/init.h> |
|---|
| 12 | 11 | #include <linux/kernel.h> |
|---|
| 13 | 12 | #include <linux/module.h> |
|---|
| 14 | | -#include <linux/i2c.h> |
|---|
| 15 | | -#include <linux/platform_data/i2c-gpio.h> |
|---|
| 16 | 13 | #include <linux/interrupt.h> |
|---|
| 17 | 14 | #include <linux/spinlock.h> |
|---|
| 18 | 15 | #include <linux/slab.h> |
|---|
| .. | .. |
|---|
| 20 | 17 | #include <media/v4l2-device.h> |
|---|
| 21 | 18 | #include <linux/platform_data/media/mmp-camera.h> |
|---|
| 22 | 19 | #include <linux/device.h> |
|---|
| 20 | +#include <linux/of.h> |
|---|
| 21 | +#include <linux/of_platform.h> |
|---|
| 23 | 22 | #include <linux/platform_device.h> |
|---|
| 24 | | -#include <linux/gpio.h> |
|---|
| 23 | +#include <linux/pm_runtime.h> |
|---|
| 25 | 24 | #include <linux/io.h> |
|---|
| 26 | | -#include <linux/delay.h> |
|---|
| 27 | 25 | #include <linux/list.h> |
|---|
| 28 | 26 | #include <linux/pm.h> |
|---|
| 29 | 27 | #include <linux/clk.h> |
|---|
| .. | .. |
|---|
| 34 | 32 | MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>"); |
|---|
| 35 | 33 | MODULE_LICENSE("GPL"); |
|---|
| 36 | 34 | |
|---|
| 37 | | -static char *mcam_clks[] = {"CCICAXICLK", "CCICFUNCLK", "CCICPHYCLK"}; |
|---|
| 35 | +static char *mcam_clks[] = {"axi", "func", "phy"}; |
|---|
| 38 | 36 | |
|---|
| 39 | 37 | struct mmp_camera { |
|---|
| 40 | | - void __iomem *power_regs; |
|---|
| 41 | 38 | struct platform_device *pdev; |
|---|
| 42 | 39 | struct mcam_camera mcam; |
|---|
| 43 | 40 | struct list_head devlist; |
|---|
| .. | .. |
|---|
| 48 | 45 | static inline struct mmp_camera *mcam_to_cam(struct mcam_camera *mcam) |
|---|
| 49 | 46 | { |
|---|
| 50 | 47 | return container_of(mcam, struct mmp_camera, mcam); |
|---|
| 51 | | -} |
|---|
| 52 | | - |
|---|
| 53 | | -/* |
|---|
| 54 | | - * A silly little infrastructure so we can keep track of our devices. |
|---|
| 55 | | - * Chances are that we will never have more than one of them, but |
|---|
| 56 | | - * the Armada 610 *does* have two controllers... |
|---|
| 57 | | - */ |
|---|
| 58 | | - |
|---|
| 59 | | -static LIST_HEAD(mmpcam_devices); |
|---|
| 60 | | -static struct mutex mmpcam_devices_lock; |
|---|
| 61 | | - |
|---|
| 62 | | -static void mmpcam_add_device(struct mmp_camera *cam) |
|---|
| 63 | | -{ |
|---|
| 64 | | - mutex_lock(&mmpcam_devices_lock); |
|---|
| 65 | | - list_add(&cam->devlist, &mmpcam_devices); |
|---|
| 66 | | - mutex_unlock(&mmpcam_devices_lock); |
|---|
| 67 | | -} |
|---|
| 68 | | - |
|---|
| 69 | | -static void mmpcam_remove_device(struct mmp_camera *cam) |
|---|
| 70 | | -{ |
|---|
| 71 | | - mutex_lock(&mmpcam_devices_lock); |
|---|
| 72 | | - list_del(&cam->devlist); |
|---|
| 73 | | - mutex_unlock(&mmpcam_devices_lock); |
|---|
| 74 | | -} |
|---|
| 75 | | - |
|---|
| 76 | | -/* |
|---|
| 77 | | - * Platform dev remove passes us a platform_device, and there's |
|---|
| 78 | | - * no handy unused drvdata to stash a backpointer in. So just |
|---|
| 79 | | - * dig it out of our list. |
|---|
| 80 | | - */ |
|---|
| 81 | | -static struct mmp_camera *mmpcam_find_device(struct platform_device *pdev) |
|---|
| 82 | | -{ |
|---|
| 83 | | - struct mmp_camera *cam; |
|---|
| 84 | | - |
|---|
| 85 | | - mutex_lock(&mmpcam_devices_lock); |
|---|
| 86 | | - list_for_each_entry(cam, &mmpcam_devices, devlist) { |
|---|
| 87 | | - if (cam->pdev == pdev) { |
|---|
| 88 | | - mutex_unlock(&mmpcam_devices_lock); |
|---|
| 89 | | - return cam; |
|---|
| 90 | | - } |
|---|
| 91 | | - } |
|---|
| 92 | | - mutex_unlock(&mmpcam_devices_lock); |
|---|
| 93 | | - return NULL; |
|---|
| 94 | | -} |
|---|
| 95 | | - |
|---|
| 96 | | - |
|---|
| 97 | | - |
|---|
| 98 | | - |
|---|
| 99 | | -/* |
|---|
| 100 | | - * Power-related registers; this almost certainly belongs |
|---|
| 101 | | - * somewhere else. |
|---|
| 102 | | - * |
|---|
| 103 | | - * ARMADA 610 register manual, sec 7.2.1, p1842. |
|---|
| 104 | | - */ |
|---|
| 105 | | -#define CPU_SUBSYS_PMU_BASE 0xd4282800 |
|---|
| 106 | | -#define REG_CCIC_DCGCR 0x28 /* CCIC dyn clock gate ctrl reg */ |
|---|
| 107 | | -#define REG_CCIC_CRCR 0x50 /* CCIC clk reset ctrl reg */ |
|---|
| 108 | | -#define REG_CCIC2_CRCR 0xf4 /* CCIC2 clk reset ctrl reg */ |
|---|
| 109 | | - |
|---|
| 110 | | -static void mcam_clk_enable(struct mcam_camera *mcam) |
|---|
| 111 | | -{ |
|---|
| 112 | | - unsigned int i; |
|---|
| 113 | | - |
|---|
| 114 | | - for (i = 0; i < NR_MCAM_CLK; i++) { |
|---|
| 115 | | - if (!IS_ERR(mcam->clk[i])) |
|---|
| 116 | | - clk_prepare_enable(mcam->clk[i]); |
|---|
| 117 | | - } |
|---|
| 118 | | -} |
|---|
| 119 | | - |
|---|
| 120 | | -static void mcam_clk_disable(struct mcam_camera *mcam) |
|---|
| 121 | | -{ |
|---|
| 122 | | - int i; |
|---|
| 123 | | - |
|---|
| 124 | | - for (i = NR_MCAM_CLK - 1; i >= 0; i--) { |
|---|
| 125 | | - if (!IS_ERR(mcam->clk[i])) |
|---|
| 126 | | - clk_disable_unprepare(mcam->clk[i]); |
|---|
| 127 | | - } |
|---|
| 128 | | -} |
|---|
| 129 | | - |
|---|
| 130 | | -/* |
|---|
| 131 | | - * Power control. |
|---|
| 132 | | - */ |
|---|
| 133 | | -static void mmpcam_power_up_ctlr(struct mmp_camera *cam) |
|---|
| 134 | | -{ |
|---|
| 135 | | - iowrite32(0x3f, cam->power_regs + REG_CCIC_DCGCR); |
|---|
| 136 | | - iowrite32(0x3805b, cam->power_regs + REG_CCIC_CRCR); |
|---|
| 137 | | - mdelay(1); |
|---|
| 138 | | -} |
|---|
| 139 | | - |
|---|
| 140 | | -static int mmpcam_power_up(struct mcam_camera *mcam) |
|---|
| 141 | | -{ |
|---|
| 142 | | - struct mmp_camera *cam = mcam_to_cam(mcam); |
|---|
| 143 | | - struct mmp_camera_platform_data *pdata; |
|---|
| 144 | | - |
|---|
| 145 | | -/* |
|---|
| 146 | | - * Turn on power and clocks to the controller. |
|---|
| 147 | | - */ |
|---|
| 148 | | - mmpcam_power_up_ctlr(cam); |
|---|
| 149 | | -/* |
|---|
| 150 | | - * Provide power to the sensor. |
|---|
| 151 | | - */ |
|---|
| 152 | | - mcam_reg_write(mcam, REG_CLKCTRL, 0x60000002); |
|---|
| 153 | | - pdata = cam->pdev->dev.platform_data; |
|---|
| 154 | | - gpio_set_value(pdata->sensor_power_gpio, 1); |
|---|
| 155 | | - mdelay(5); |
|---|
| 156 | | - mcam_reg_clear_bit(mcam, REG_CTRL1, 0x10000000); |
|---|
| 157 | | - gpio_set_value(pdata->sensor_reset_gpio, 0); /* reset is active low */ |
|---|
| 158 | | - mdelay(5); |
|---|
| 159 | | - gpio_set_value(pdata->sensor_reset_gpio, 1); /* reset is active low */ |
|---|
| 160 | | - mdelay(5); |
|---|
| 161 | | - |
|---|
| 162 | | - mcam_clk_enable(mcam); |
|---|
| 163 | | - |
|---|
| 164 | | - return 0; |
|---|
| 165 | | -} |
|---|
| 166 | | - |
|---|
| 167 | | -static void mmpcam_power_down(struct mcam_camera *mcam) |
|---|
| 168 | | -{ |
|---|
| 169 | | - struct mmp_camera *cam = mcam_to_cam(mcam); |
|---|
| 170 | | - struct mmp_camera_platform_data *pdata; |
|---|
| 171 | | -/* |
|---|
| 172 | | - * Turn off clocks and set reset lines |
|---|
| 173 | | - */ |
|---|
| 174 | | - iowrite32(0, cam->power_regs + REG_CCIC_DCGCR); |
|---|
| 175 | | - iowrite32(0, cam->power_regs + REG_CCIC_CRCR); |
|---|
| 176 | | -/* |
|---|
| 177 | | - * Shut down the sensor. |
|---|
| 178 | | - */ |
|---|
| 179 | | - pdata = cam->pdev->dev.platform_data; |
|---|
| 180 | | - gpio_set_value(pdata->sensor_power_gpio, 0); |
|---|
| 181 | | - gpio_set_value(pdata->sensor_reset_gpio, 0); |
|---|
| 182 | | - |
|---|
| 183 | | - mcam_clk_disable(mcam); |
|---|
| 184 | | -} |
|---|
| 185 | | - |
|---|
| 186 | | -static void mcam_ctlr_reset(struct mcam_camera *mcam) |
|---|
| 187 | | -{ |
|---|
| 188 | | - unsigned long val; |
|---|
| 189 | | - struct mmp_camera *cam = mcam_to_cam(mcam); |
|---|
| 190 | | - |
|---|
| 191 | | - if (mcam->ccic_id) { |
|---|
| 192 | | - /* |
|---|
| 193 | | - * Using CCIC2 |
|---|
| 194 | | - */ |
|---|
| 195 | | - val = ioread32(cam->power_regs + REG_CCIC2_CRCR); |
|---|
| 196 | | - iowrite32(val & ~0x2, cam->power_regs + REG_CCIC2_CRCR); |
|---|
| 197 | | - iowrite32(val | 0x2, cam->power_regs + REG_CCIC2_CRCR); |
|---|
| 198 | | - } else { |
|---|
| 199 | | - /* |
|---|
| 200 | | - * Using CCIC1 |
|---|
| 201 | | - */ |
|---|
| 202 | | - val = ioread32(cam->power_regs + REG_CCIC_CRCR); |
|---|
| 203 | | - iowrite32(val & ~0x2, cam->power_regs + REG_CCIC_CRCR); |
|---|
| 204 | | - iowrite32(val | 0x2, cam->power_regs + REG_CCIC_CRCR); |
|---|
| 205 | | - } |
|---|
| 206 | 48 | } |
|---|
| 207 | 49 | |
|---|
| 208 | 50 | /* |
|---|
| .. | .. |
|---|
| 240 | 82 | * bit 8 ~ bit 15: HS_SETTLE |
|---|
| 241 | 83 | * Time interval during which the HS |
|---|
| 242 | 84 | * receiver shall ignore any Data Lane |
|---|
| 243 | | - * HS transistions. |
|---|
| 244 | | - * The vaule has been calibrated on |
|---|
| 85 | + * HS transitions. |
|---|
| 86 | + * The value has been calibrated on |
|---|
| 245 | 87 | * different boards. It seems to work well. |
|---|
| 246 | 88 | * |
|---|
| 247 | 89 | * More detail please refer |
|---|
| .. | .. |
|---|
| 336 | 178 | struct mmp_camera *cam; |
|---|
| 337 | 179 | struct mcam_camera *mcam; |
|---|
| 338 | 180 | struct resource *res; |
|---|
| 181 | + struct fwnode_handle *ep; |
|---|
| 339 | 182 | struct mmp_camera_platform_data *pdata; |
|---|
| 340 | 183 | int ret; |
|---|
| 341 | | - |
|---|
| 342 | | - pdata = pdev->dev.platform_data; |
|---|
| 343 | | - if (!pdata) |
|---|
| 344 | | - return -ENODEV; |
|---|
| 345 | 184 | |
|---|
| 346 | 185 | cam = devm_kzalloc(&pdev->dev, sizeof(*cam), GFP_KERNEL); |
|---|
| 347 | 186 | if (cam == NULL) |
|---|
| 348 | 187 | return -ENOMEM; |
|---|
| 188 | + platform_set_drvdata(pdev, cam); |
|---|
| 349 | 189 | cam->pdev = pdev; |
|---|
| 350 | 190 | INIT_LIST_HEAD(&cam->devlist); |
|---|
| 351 | 191 | |
|---|
| 352 | 192 | mcam = &cam->mcam; |
|---|
| 353 | | - mcam->plat_power_up = mmpcam_power_up; |
|---|
| 354 | | - mcam->plat_power_down = mmpcam_power_down; |
|---|
| 355 | | - mcam->ctlr_reset = mcam_ctlr_reset; |
|---|
| 356 | 193 | mcam->calc_dphy = mmpcam_calc_dphy; |
|---|
| 357 | 194 | mcam->dev = &pdev->dev; |
|---|
| 358 | | - mcam->use_smbus = 0; |
|---|
| 359 | | - mcam->ccic_id = pdev->id; |
|---|
| 360 | | - mcam->mclk_min = pdata->mclk_min; |
|---|
| 361 | | - mcam->mclk_src = pdata->mclk_src; |
|---|
| 362 | | - mcam->mclk_div = pdata->mclk_div; |
|---|
| 363 | | - mcam->bus_type = pdata->bus_type; |
|---|
| 364 | | - mcam->dphy = pdata->dphy; |
|---|
| 365 | | - if (mcam->bus_type == V4L2_MBUS_CSI2) { |
|---|
| 195 | + pdata = pdev->dev.platform_data; |
|---|
| 196 | + if (pdata) { |
|---|
| 197 | + mcam->mclk_src = pdata->mclk_src; |
|---|
| 198 | + mcam->mclk_div = pdata->mclk_div; |
|---|
| 199 | + mcam->bus_type = pdata->bus_type; |
|---|
| 200 | + mcam->dphy = pdata->dphy; |
|---|
| 201 | + mcam->lane = pdata->lane; |
|---|
| 202 | + } else { |
|---|
| 203 | + /* |
|---|
| 204 | + * These are values that used to be hardcoded in mcam-core and |
|---|
| 205 | + * work well on a OLPC XO 1.75 with a parallel bus sensor. |
|---|
| 206 | + * If it turns out other setups make sense, the values should |
|---|
| 207 | + * be obtained from the device tree. |
|---|
| 208 | + */ |
|---|
| 209 | + mcam->mclk_src = 3; |
|---|
| 210 | + mcam->mclk_div = 2; |
|---|
| 211 | + } |
|---|
| 212 | + if (mcam->bus_type == V4L2_MBUS_CSI2_DPHY) { |
|---|
| 366 | 213 | cam->mipi_clk = devm_clk_get(mcam->dev, "mipi"); |
|---|
| 367 | 214 | if ((IS_ERR(cam->mipi_clk) && mcam->dphy[2] == 0)) |
|---|
| 368 | 215 | return PTR_ERR(cam->mipi_clk); |
|---|
| 369 | 216 | } |
|---|
| 370 | 217 | mcam->mipi_enabled = false; |
|---|
| 371 | | - mcam->lane = pdata->lane; |
|---|
| 372 | 218 | mcam->chip_id = MCAM_ARMADA610; |
|---|
| 373 | 219 | mcam->buffer_mode = B_DMA_sg; |
|---|
| 374 | | - strlcpy(mcam->bus_info, "platform:mmp-camera", sizeof(mcam->bus_info)); |
|---|
| 220 | + strscpy(mcam->bus_info, "platform:mmp-camera", sizeof(mcam->bus_info)); |
|---|
| 375 | 221 | spin_lock_init(&mcam->dev_lock); |
|---|
| 376 | 222 | /* |
|---|
| 377 | 223 | * Get our I/O memory. |
|---|
| .. | .. |
|---|
| 381 | 227 | if (IS_ERR(mcam->regs)) |
|---|
| 382 | 228 | return PTR_ERR(mcam->regs); |
|---|
| 383 | 229 | mcam->regs_size = resource_size(res); |
|---|
| 384 | | - /* |
|---|
| 385 | | - * Power/clock memory is elsewhere; get it too. Perhaps this |
|---|
| 386 | | - * should really be managed outside of this driver? |
|---|
| 387 | | - */ |
|---|
| 388 | | - res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
|---|
| 389 | | - cam->power_regs = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 390 | | - if (IS_ERR(cam->power_regs)) |
|---|
| 391 | | - return PTR_ERR(cam->power_regs); |
|---|
| 392 | | - /* |
|---|
| 393 | | - * Find the i2c adapter. This assumes, of course, that the |
|---|
| 394 | | - * i2c bus is already up and functioning. |
|---|
| 395 | | - */ |
|---|
| 396 | | - mcam->i2c_adapter = platform_get_drvdata(pdata->i2c_device); |
|---|
| 397 | | - if (mcam->i2c_adapter == NULL) { |
|---|
| 398 | | - dev_err(&pdev->dev, "No i2c adapter\n"); |
|---|
| 399 | | - return -ENODEV; |
|---|
| 400 | | - } |
|---|
| 401 | | - /* |
|---|
| 402 | | - * Sensor GPIO pins. |
|---|
| 403 | | - */ |
|---|
| 404 | | - ret = devm_gpio_request(&pdev->dev, pdata->sensor_power_gpio, |
|---|
| 405 | | - "cam-power"); |
|---|
| 406 | | - if (ret) { |
|---|
| 407 | | - dev_err(&pdev->dev, "Can't get sensor power gpio %d", |
|---|
| 408 | | - pdata->sensor_power_gpio); |
|---|
| 409 | | - return ret; |
|---|
| 410 | | - } |
|---|
| 411 | | - gpio_direction_output(pdata->sensor_power_gpio, 0); |
|---|
| 412 | | - ret = devm_gpio_request(&pdev->dev, pdata->sensor_reset_gpio, |
|---|
| 413 | | - "cam-reset"); |
|---|
| 414 | | - if (ret) { |
|---|
| 415 | | - dev_err(&pdev->dev, "Can't get sensor reset gpio %d", |
|---|
| 416 | | - pdata->sensor_reset_gpio); |
|---|
| 417 | | - return ret; |
|---|
| 418 | | - } |
|---|
| 419 | | - gpio_direction_output(pdata->sensor_reset_gpio, 0); |
|---|
| 420 | 230 | |
|---|
| 421 | 231 | mcam_init_clk(mcam); |
|---|
| 422 | 232 | |
|---|
| 423 | 233 | /* |
|---|
| 424 | | - * Power the device up and hand it off to the core. |
|---|
| 234 | + * Create a match of the sensor against its OF node. |
|---|
| 425 | 235 | */ |
|---|
| 426 | | - ret = mmpcam_power_up(mcam); |
|---|
| 427 | | - if (ret) |
|---|
| 428 | | - return ret; |
|---|
| 236 | + ep = fwnode_graph_get_next_endpoint(of_fwnode_handle(pdev->dev.of_node), |
|---|
| 237 | + NULL); |
|---|
| 238 | + if (!ep) |
|---|
| 239 | + return -ENODEV; |
|---|
| 240 | + |
|---|
| 241 | + mcam->asd.match_type = V4L2_ASYNC_MATCH_FWNODE; |
|---|
| 242 | + mcam->asd.match.fwnode = fwnode_graph_get_remote_port_parent(ep); |
|---|
| 243 | + |
|---|
| 244 | + fwnode_handle_put(ep); |
|---|
| 245 | + |
|---|
| 246 | + /* |
|---|
| 247 | + * Register the device with the core. |
|---|
| 248 | + */ |
|---|
| 429 | 249 | ret = mccic_register(mcam); |
|---|
| 430 | 250 | if (ret) |
|---|
| 431 | | - goto out_power_down; |
|---|
| 251 | + return ret; |
|---|
| 252 | + |
|---|
| 253 | + /* |
|---|
| 254 | + * Add OF clock provider. |
|---|
| 255 | + */ |
|---|
| 256 | + ret = of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get, |
|---|
| 257 | + mcam->mclk); |
|---|
| 258 | + if (ret) { |
|---|
| 259 | + dev_err(&pdev->dev, "can't add DT clock provider\n"); |
|---|
| 260 | + goto out; |
|---|
| 261 | + } |
|---|
| 262 | + |
|---|
| 432 | 263 | /* |
|---|
| 433 | 264 | * Finally, set up our IRQ now that the core is ready to |
|---|
| 434 | 265 | * deal with it. |
|---|
| .. | .. |
|---|
| 436 | 267 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
|---|
| 437 | 268 | if (res == NULL) { |
|---|
| 438 | 269 | ret = -ENODEV; |
|---|
| 439 | | - goto out_unregister; |
|---|
| 270 | + goto out; |
|---|
| 440 | 271 | } |
|---|
| 441 | 272 | cam->irq = res->start; |
|---|
| 442 | 273 | ret = devm_request_irq(&pdev->dev, cam->irq, mmpcam_irq, IRQF_SHARED, |
|---|
| 443 | 274 | "mmp-camera", mcam); |
|---|
| 444 | | - if (ret == 0) { |
|---|
| 445 | | - mmpcam_add_device(cam); |
|---|
| 446 | | - return 0; |
|---|
| 447 | | - } |
|---|
| 275 | + if (ret) |
|---|
| 276 | + goto out; |
|---|
| 448 | 277 | |
|---|
| 449 | | -out_unregister: |
|---|
| 278 | + pm_runtime_enable(&pdev->dev); |
|---|
| 279 | + return 0; |
|---|
| 280 | +out: |
|---|
| 281 | + fwnode_handle_put(mcam->asd.match.fwnode); |
|---|
| 450 | 282 | mccic_shutdown(mcam); |
|---|
| 451 | | -out_power_down: |
|---|
| 452 | | - mmpcam_power_down(mcam); |
|---|
| 283 | + |
|---|
| 453 | 284 | return ret; |
|---|
| 454 | 285 | } |
|---|
| 455 | 286 | |
|---|
| .. | .. |
|---|
| 458 | 289 | { |
|---|
| 459 | 290 | struct mcam_camera *mcam = &cam->mcam; |
|---|
| 460 | 291 | |
|---|
| 461 | | - mmpcam_remove_device(cam); |
|---|
| 462 | 292 | mccic_shutdown(mcam); |
|---|
| 463 | | - mmpcam_power_down(mcam); |
|---|
| 293 | + pm_runtime_force_suspend(mcam->dev); |
|---|
| 464 | 294 | return 0; |
|---|
| 465 | 295 | } |
|---|
| 466 | 296 | |
|---|
| 467 | 297 | static int mmpcam_platform_remove(struct platform_device *pdev) |
|---|
| 468 | 298 | { |
|---|
| 469 | | - struct mmp_camera *cam = mmpcam_find_device(pdev); |
|---|
| 299 | + struct mmp_camera *cam = platform_get_drvdata(pdev); |
|---|
| 470 | 300 | |
|---|
| 471 | 301 | if (cam == NULL) |
|---|
| 472 | 302 | return -ENODEV; |
|---|
| .. | .. |
|---|
| 476 | 306 | /* |
|---|
| 477 | 307 | * Suspend/resume support. |
|---|
| 478 | 308 | */ |
|---|
| 309 | + |
|---|
| 479 | 310 | #ifdef CONFIG_PM |
|---|
| 480 | | - |
|---|
| 481 | | -static int mmpcam_suspend(struct platform_device *pdev, pm_message_t state) |
|---|
| 311 | +static int mmpcam_runtime_resume(struct device *dev) |
|---|
| 482 | 312 | { |
|---|
| 483 | | - struct mmp_camera *cam = mmpcam_find_device(pdev); |
|---|
| 313 | + struct mmp_camera *cam = dev_get_drvdata(dev); |
|---|
| 314 | + struct mcam_camera *mcam = &cam->mcam; |
|---|
| 315 | + unsigned int i; |
|---|
| 484 | 316 | |
|---|
| 485 | | - if (state.event != PM_EVENT_SUSPEND) |
|---|
| 486 | | - return 0; |
|---|
| 487 | | - mccic_suspend(&cam->mcam); |
|---|
| 317 | + for (i = 0; i < NR_MCAM_CLK; i++) { |
|---|
| 318 | + if (!IS_ERR(mcam->clk[i])) |
|---|
| 319 | + clk_prepare_enable(mcam->clk[i]); |
|---|
| 320 | + } |
|---|
| 321 | + |
|---|
| 488 | 322 | return 0; |
|---|
| 489 | 323 | } |
|---|
| 490 | 324 | |
|---|
| 491 | | -static int mmpcam_resume(struct platform_device *pdev) |
|---|
| 325 | +static int mmpcam_runtime_suspend(struct device *dev) |
|---|
| 492 | 326 | { |
|---|
| 493 | | - struct mmp_camera *cam = mmpcam_find_device(pdev); |
|---|
| 327 | + struct mmp_camera *cam = dev_get_drvdata(dev); |
|---|
| 328 | + struct mcam_camera *mcam = &cam->mcam; |
|---|
| 329 | + int i; |
|---|
| 494 | 330 | |
|---|
| 495 | | - /* |
|---|
| 496 | | - * Power up unconditionally just in case the core tries to |
|---|
| 497 | | - * touch a register even if nothing was active before; trust |
|---|
| 498 | | - * me, it's better this way. |
|---|
| 499 | | - */ |
|---|
| 500 | | - mmpcam_power_up_ctlr(cam); |
|---|
| 501 | | - return mccic_resume(&cam->mcam); |
|---|
| 331 | + for (i = NR_MCAM_CLK - 1; i >= 0; i--) { |
|---|
| 332 | + if (!IS_ERR(mcam->clk[i])) |
|---|
| 333 | + clk_disable_unprepare(mcam->clk[i]); |
|---|
| 334 | + } |
|---|
| 335 | + |
|---|
| 336 | + return 0; |
|---|
| 502 | 337 | } |
|---|
| 503 | 338 | |
|---|
| 339 | +static int __maybe_unused mmpcam_suspend(struct device *dev) |
|---|
| 340 | +{ |
|---|
| 341 | + struct mmp_camera *cam = dev_get_drvdata(dev); |
|---|
| 342 | + |
|---|
| 343 | + if (!pm_runtime_suspended(dev)) |
|---|
| 344 | + mccic_suspend(&cam->mcam); |
|---|
| 345 | + return 0; |
|---|
| 346 | +} |
|---|
| 347 | + |
|---|
| 348 | +static int __maybe_unused mmpcam_resume(struct device *dev) |
|---|
| 349 | +{ |
|---|
| 350 | + struct mmp_camera *cam = dev_get_drvdata(dev); |
|---|
| 351 | + |
|---|
| 352 | + if (!pm_runtime_suspended(dev)) |
|---|
| 353 | + return mccic_resume(&cam->mcam); |
|---|
| 354 | + return 0; |
|---|
| 355 | +} |
|---|
| 504 | 356 | #endif |
|---|
| 505 | 357 | |
|---|
| 358 | +static const struct dev_pm_ops mmpcam_pm_ops = { |
|---|
| 359 | + SET_RUNTIME_PM_OPS(mmpcam_runtime_suspend, mmpcam_runtime_resume, NULL) |
|---|
| 360 | + SET_SYSTEM_SLEEP_PM_OPS(mmpcam_suspend, mmpcam_resume) |
|---|
| 361 | +}; |
|---|
| 362 | + |
|---|
| 363 | +static const struct of_device_id mmpcam_of_match[] = { |
|---|
| 364 | + { .compatible = "marvell,mmp2-ccic", }, |
|---|
| 365 | + {}, |
|---|
| 366 | +}; |
|---|
| 367 | +MODULE_DEVICE_TABLE(of, mmpcam_of_match); |
|---|
| 506 | 368 | |
|---|
| 507 | 369 | static struct platform_driver mmpcam_driver = { |
|---|
| 508 | 370 | .probe = mmpcam_probe, |
|---|
| 509 | 371 | .remove = mmpcam_platform_remove, |
|---|
| 510 | | -#ifdef CONFIG_PM |
|---|
| 511 | | - .suspend = mmpcam_suspend, |
|---|
| 512 | | - .resume = mmpcam_resume, |
|---|
| 513 | | -#endif |
|---|
| 514 | 372 | .driver = { |
|---|
| 515 | 373 | .name = "mmp-camera", |
|---|
| 374 | + .of_match_table = of_match_ptr(mmpcam_of_match), |
|---|
| 375 | + .pm = &mmpcam_pm_ops, |
|---|
| 516 | 376 | } |
|---|
| 517 | 377 | }; |
|---|
| 518 | 378 | |
|---|
| 519 | | - |
|---|
| 520 | | -static int __init mmpcam_init_module(void) |
|---|
| 521 | | -{ |
|---|
| 522 | | - mutex_init(&mmpcam_devices_lock); |
|---|
| 523 | | - return platform_driver_register(&mmpcam_driver); |
|---|
| 524 | | -} |
|---|
| 525 | | - |
|---|
| 526 | | -static void __exit mmpcam_exit_module(void) |
|---|
| 527 | | -{ |
|---|
| 528 | | - platform_driver_unregister(&mmpcam_driver); |
|---|
| 529 | | - /* |
|---|
| 530 | | - * platform_driver_unregister() should have emptied the list |
|---|
| 531 | | - */ |
|---|
| 532 | | - if (!list_empty(&mmpcam_devices)) |
|---|
| 533 | | - printk(KERN_ERR "mmp_camera leaving devices behind\n"); |
|---|
| 534 | | -} |
|---|
| 535 | | - |
|---|
| 536 | | -module_init(mmpcam_init_module); |
|---|
| 537 | | -module_exit(mmpcam_exit_module); |
|---|
| 379 | +module_platform_driver(mmpcam_driver); |
|---|