1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2016 NextThing Co 4 * Copyright (C) 2016-2019 Bootlin 5 * 6 * Author: Maxime Ripard <maxime.ripard@bootlin.com> 7 */ 8 9 #include <linux/clk.h> 10 #include <linux/dma-mapping.h> 11 #include <linux/interrupt.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/of.h> 15 #include <linux/of_graph.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/reset.h> 19 #include <linux/videodev2.h> 20 21 #include <media/v4l2-dev.h> 22 #include <media/v4l2-device.h> 23 #include <media/v4l2-fwnode.h> 24 #include <media/v4l2-ioctl.h> 25 #include <media/v4l2-mediabus.h> 26 27 #include <media/videobuf2-core.h> 28 #include <media/videobuf2-dma-contig.h> 29 30 #include "sun4i_csi.h" 31 32 struct sun4i_csi_traits { 33 unsigned int channels; 34 unsigned int max_width; 35 bool has_isp; 36 }; 37 38 static const struct media_entity_operations sun4i_csi_video_entity_ops = { 39 .link_validate = v4l2_subdev_link_validate, 40 }; 41 42 static const struct media_entity_operations sun4i_csi_subdev_entity_ops = { 43 .link_validate = v4l2_subdev_link_validate, 44 }; 45 46 static int sun4i_csi_notify_bound(struct v4l2_async_notifier *notifier, 47 struct v4l2_subdev *subdev, 48 struct v4l2_async_connection *asd) 49 { 50 struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi, 51 notifier); 52 53 csi->src_subdev = subdev; 54 csi->src_pad = media_entity_get_fwnode_pad(&subdev->entity, 55 subdev->fwnode, 56 MEDIA_PAD_FL_SOURCE); 57 if (csi->src_pad < 0) { 58 dev_err(csi->dev, "Couldn't find output pad for subdev %s\n", 59 subdev->name); 60 return csi->src_pad; 61 } 62 63 dev_dbg(csi->dev, "Bound %s pad: %d\n", subdev->name, csi->src_pad); 64 return 0; 65 } 66 67 static int sun4i_csi_notify_complete(struct v4l2_async_notifier *notifier) 68 { 69 struct sun4i_csi *csi = container_of(notifier, struct sun4i_csi, 70 notifier); 71 struct v4l2_subdev *subdev = &csi->subdev; 72 struct video_device *vdev = &csi->vdev; 73 int ret; 74 75 ret = v4l2_device_register_subdev(&csi->v4l, subdev); 76 if (ret < 0) 77 return ret; 78 79 ret = sun4i_csi_v4l2_register(csi); 80 if (ret < 0) 81 return ret; 82 83 ret = media_device_register(&csi->mdev); 84 if (ret) 85 return ret; 86 87 /* Create link from subdev to main device */ 88 ret = media_create_pad_link(&subdev->entity, CSI_SUBDEV_SOURCE, 89 &vdev->entity, 0, 90 MEDIA_LNK_FL_ENABLED | 91 MEDIA_LNK_FL_IMMUTABLE); 92 if (ret) 93 goto err_clean_media; 94 95 ret = media_create_pad_link(&csi->src_subdev->entity, csi->src_pad, 96 &subdev->entity, CSI_SUBDEV_SINK, 97 MEDIA_LNK_FL_ENABLED | 98 MEDIA_LNK_FL_IMMUTABLE); 99 if (ret) 100 goto err_clean_media; 101 102 ret = v4l2_device_register_subdev_nodes(&csi->v4l); 103 if (ret < 0) 104 goto err_clean_media; 105 106 return 0; 107 108 err_clean_media: 109 media_device_unregister(&csi->mdev); 110 111 return ret; 112 } 113 114 static const struct v4l2_async_notifier_operations sun4i_csi_notify_ops = { 115 .bound = sun4i_csi_notify_bound, 116 .complete = sun4i_csi_notify_complete, 117 }; 118 119 static int sun4i_csi_notifier_init(struct sun4i_csi *csi) 120 { 121 struct v4l2_fwnode_endpoint vep = { 122 .bus_type = V4L2_MBUS_PARALLEL, 123 }; 124 struct v4l2_async_connection *asd; 125 struct fwnode_handle *ep; 126 int ret; 127 128 v4l2_async_nf_init(&csi->notifier, &csi->v4l); 129 130 ep = fwnode_graph_get_endpoint_by_id(dev_fwnode(csi->dev), 0, 0, 131 FWNODE_GRAPH_ENDPOINT_NEXT); 132 if (!ep) 133 return -EINVAL; 134 135 ret = v4l2_fwnode_endpoint_parse(ep, &vep); 136 if (ret) 137 goto out; 138 139 csi->bus = vep.bus.parallel; 140 141 asd = v4l2_async_nf_add_fwnode_remote(&csi->notifier, ep, 142 struct v4l2_async_connection); 143 if (IS_ERR(asd)) { 144 ret = PTR_ERR(asd); 145 goto out; 146 } 147 148 csi->notifier.ops = &sun4i_csi_notify_ops; 149 150 out: 151 fwnode_handle_put(ep); 152 return ret; 153 } 154 155 static int sun4i_csi_probe(struct platform_device *pdev) 156 { 157 struct v4l2_subdev *subdev; 158 struct video_device *vdev; 159 struct sun4i_csi *csi; 160 int ret; 161 int irq; 162 163 csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL); 164 if (!csi) 165 return -ENOMEM; 166 platform_set_drvdata(pdev, csi); 167 csi->dev = &pdev->dev; 168 subdev = &csi->subdev; 169 vdev = &csi->vdev; 170 171 csi->traits = of_device_get_match_data(&pdev->dev); 172 if (!csi->traits) 173 return -EINVAL; 174 175 csi->mdev.dev = csi->dev; 176 strscpy(csi->mdev.model, "Allwinner Video Capture Device", 177 sizeof(csi->mdev.model)); 178 csi->mdev.hw_revision = 0; 179 media_device_init(&csi->mdev); 180 csi->v4l.mdev = &csi->mdev; 181 182 csi->regs = devm_platform_ioremap_resource(pdev, 0); 183 if (IS_ERR(csi->regs)) 184 return PTR_ERR(csi->regs); 185 186 irq = platform_get_irq(pdev, 0); 187 if (irq < 0) 188 return irq; 189 190 csi->bus_clk = devm_clk_get(&pdev->dev, "bus"); 191 if (IS_ERR(csi->bus_clk)) { 192 dev_err(&pdev->dev, "Couldn't get our bus clock\n"); 193 return PTR_ERR(csi->bus_clk); 194 } 195 196 if (csi->traits->has_isp) { 197 csi->isp_clk = devm_clk_get(&pdev->dev, "isp"); 198 if (IS_ERR(csi->isp_clk)) { 199 dev_err(&pdev->dev, "Couldn't get our ISP clock\n"); 200 return PTR_ERR(csi->isp_clk); 201 } 202 } 203 204 csi->ram_clk = devm_clk_get(&pdev->dev, "ram"); 205 if (IS_ERR(csi->ram_clk)) { 206 dev_err(&pdev->dev, "Couldn't get our ram clock\n"); 207 return PTR_ERR(csi->ram_clk); 208 } 209 210 csi->rst = devm_reset_control_get(&pdev->dev, NULL); 211 if (IS_ERR(csi->rst)) { 212 dev_err(&pdev->dev, "Couldn't get our reset line\n"); 213 return PTR_ERR(csi->rst); 214 } 215 216 /* Initialize subdev */ 217 v4l2_subdev_init(subdev, &sun4i_csi_subdev_ops); 218 subdev->flags = V4L2_SUBDEV_FL_HAS_DEVNODE | V4L2_SUBDEV_FL_HAS_EVENTS; 219 subdev->entity.function = MEDIA_ENT_F_VID_IF_BRIDGE; 220 subdev->entity.ops = &sun4i_csi_subdev_entity_ops; 221 subdev->owner = THIS_MODULE; 222 snprintf(subdev->name, sizeof(subdev->name), "sun4i-csi-0"); 223 v4l2_set_subdevdata(subdev, csi); 224 225 csi->subdev_pads[CSI_SUBDEV_SINK].flags = 226 MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT; 227 csi->subdev_pads[CSI_SUBDEV_SOURCE].flags = MEDIA_PAD_FL_SOURCE; 228 ret = media_entity_pads_init(&subdev->entity, CSI_SUBDEV_PADS, 229 csi->subdev_pads); 230 if (ret < 0) 231 return ret; 232 233 csi->vdev_pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT; 234 vdev->entity.ops = &sun4i_csi_video_entity_ops; 235 ret = media_entity_pads_init(&vdev->entity, 1, &csi->vdev_pad); 236 if (ret < 0) 237 return ret; 238 239 ret = sun4i_csi_dma_register(csi, irq); 240 if (ret) 241 goto err_clean_pad; 242 243 ret = sun4i_csi_notifier_init(csi); 244 if (ret) 245 goto err_unregister_media; 246 247 ret = v4l2_async_nf_register(&csi->notifier); 248 if (ret) { 249 dev_err(csi->dev, "Couldn't register our notifier.\n"); 250 goto err_unregister_media; 251 } 252 253 pm_runtime_enable(&pdev->dev); 254 255 return 0; 256 257 err_unregister_media: 258 media_device_unregister(&csi->mdev); 259 sun4i_csi_dma_unregister(csi); 260 261 err_clean_pad: 262 media_device_cleanup(&csi->mdev); 263 264 return ret; 265 } 266 267 static void sun4i_csi_remove(struct platform_device *pdev) 268 { 269 struct sun4i_csi *csi = platform_get_drvdata(pdev); 270 271 pm_runtime_disable(&pdev->dev); 272 v4l2_async_nf_unregister(&csi->notifier); 273 v4l2_async_nf_cleanup(&csi->notifier); 274 vb2_video_unregister_device(&csi->vdev); 275 media_device_unregister(&csi->mdev); 276 sun4i_csi_dma_unregister(csi); 277 media_device_cleanup(&csi->mdev); 278 } 279 280 static const struct sun4i_csi_traits sun4i_a10_csi1_traits = { 281 .channels = 1, 282 .max_width = 24, 283 .has_isp = false, 284 }; 285 286 static const struct sun4i_csi_traits sun7i_a20_csi0_traits = { 287 .channels = 4, 288 .max_width = 16, 289 .has_isp = true, 290 }; 291 292 static const struct of_device_id sun4i_csi_of_match[] = { 293 { .compatible = "allwinner,sun4i-a10-csi1", .data = &sun4i_a10_csi1_traits }, 294 { .compatible = "allwinner,sun7i-a20-csi0", .data = &sun7i_a20_csi0_traits }, 295 { /* Sentinel */ } 296 }; 297 MODULE_DEVICE_TABLE(of, sun4i_csi_of_match); 298 299 static int __maybe_unused sun4i_csi_runtime_resume(struct device *dev) 300 { 301 struct sun4i_csi *csi = dev_get_drvdata(dev); 302 303 reset_control_deassert(csi->rst); 304 clk_prepare_enable(csi->bus_clk); 305 clk_prepare_enable(csi->ram_clk); 306 clk_set_rate(csi->isp_clk, 80000000); 307 clk_prepare_enable(csi->isp_clk); 308 309 writel(1, csi->regs + CSI_EN_REG); 310 311 return 0; 312 } 313 314 static int __maybe_unused sun4i_csi_runtime_suspend(struct device *dev) 315 { 316 struct sun4i_csi *csi = dev_get_drvdata(dev); 317 318 clk_disable_unprepare(csi->isp_clk); 319 clk_disable_unprepare(csi->ram_clk); 320 clk_disable_unprepare(csi->bus_clk); 321 322 reset_control_assert(csi->rst); 323 324 return 0; 325 } 326 327 static const struct dev_pm_ops sun4i_csi_pm_ops = { 328 SET_RUNTIME_PM_OPS(sun4i_csi_runtime_suspend, 329 sun4i_csi_runtime_resume, 330 NULL) 331 }; 332 333 static struct platform_driver sun4i_csi_driver = { 334 .probe = sun4i_csi_probe, 335 .remove_new = sun4i_csi_remove, 336 .driver = { 337 .name = "sun4i-csi", 338 .of_match_table = sun4i_csi_of_match, 339 .pm = &sun4i_csi_pm_ops, 340 }, 341 }; 342 module_platform_driver(sun4i_csi_driver); 343 344 MODULE_DESCRIPTION("Allwinner A10 Camera Sensor Interface driver"); 345 MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>"); 346 MODULE_LICENSE("GPL"); 347