forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/media/i2c/ov2659.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Omnivision OV2659 CMOS Image Sensor driver
34 *
....@@ -5,46 +6,21 @@
56 *
67 * Benoit Parrot <bparrot@ti.com>
78 * Lad, Prabhakar <prabhakar.csengg@gmail.com>
8
- *
9
- * This program is free software; you may redistribute it and/or modify
10
- * it under the terms of the GNU General Public License as published by
11
- * the Free Software Foundation; version 2 of the License.
12
- *
13
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17
- * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18
- * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
- * SOFTWARE.
219 */
2210
2311 #include <linux/clk.h>
2412 #include <linux/delay.h>
25
-#include <linux/err.h>
26
-#include <linux/init.h>
27
-#include <linux/interrupt.h>
28
-#include <linux/io.h>
13
+#include <linux/gpio/consumer.h>
2914 #include <linux/i2c.h>
30
-#include <linux/kernel.h>
31
-#include <linux/media.h>
3215 #include <linux/module.h>
33
-#include <linux/of.h>
3416 #include <linux/of_graph.h>
35
-#include <linux/slab.h>
36
-#include <linux/uaccess.h>
37
-#include <linux/videodev2.h>
17
+#include <linux/pm_runtime.h>
3818
39
-#include <media/media-entity.h>
4019 #include <media/i2c/ov2659.h>
41
-#include <media/v4l2-common.h>
4220 #include <media/v4l2-ctrls.h>
43
-#include <media/v4l2-device.h>
4421 #include <media/v4l2-event.h>
4522 #include <media/v4l2-fwnode.h>
4623 #include <media/v4l2-image-sizes.h>
47
-#include <media/v4l2-mediabus.h>
4824 #include <media/v4l2-subdev.h>
4925
5026 #define DRIVER_NAME "ov2659"
....@@ -228,10 +204,15 @@
228204 struct i2c_client *client;
229205 struct v4l2_ctrl_handler ctrls;
230206 struct v4l2_ctrl *link_frequency;
207
+ struct clk *clk;
231208 const struct ov2659_framesize *frame_size;
232209 struct sensor_register *format_ctrl_regs;
233210 struct ov2659_pll_ctrl pll;
234211 int streaming;
212
+ /* used to control the sensor PWDN pin */
213
+ struct gpio_desc *pwdn_gpio;
214
+ /* used to control the sensor RESETB pin */
215
+ struct gpio_desc *resetb_gpio;
235216 };
236217
237218 static const struct sensor_register ov2659_init_regs[] = {
....@@ -665,7 +646,7 @@
665646 { REG_TIMING_HORIZ_FORMAT, 0x01 },
666647 { 0x370a, 0x52 },
667648 { REG_VFIFO_READ_START_H, 0x00 },
668
- { REG_VFIFO_READ_START_L, 0x80 },
649
+ { REG_VFIFO_READ_START_L, 0xa0 },
669650 { REG_ISP_CTRL02, 0x10 },
670651 { REG_NULL, 0x00 },
671652 };
....@@ -713,7 +694,7 @@
713694 { REG_TIMING_HORIZ_FORMAT, 0x01 },
714695 { 0x370a, 0x52 },
715696 { REG_VFIFO_READ_START_H, 0x00 },
716
- { REG_VFIFO_READ_START_L, 0x80 },
697
+ { REG_VFIFO_READ_START_L, 0xa0 },
717698 { REG_ISP_CTRL02, 0x10 },
718699 { REG_NULL, 0x00 },
719700 };
....@@ -1059,7 +1040,7 @@
10591040 mutex_unlock(&ov2659->lock);
10601041 return 0;
10611042 #else
1062
- return -ENOTTY;
1043
+ return -EINVAL;
10631044 #endif
10641045 }
10651046
....@@ -1135,8 +1116,6 @@
11351116 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
11361117 mf = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
11371118 *mf = fmt->format;
1138
-#else
1139
- ret = -ENOTTY;
11401119 #endif
11411120 } else {
11421121 s64 val;
....@@ -1204,10 +1183,19 @@
12041183 /* Stop Streaming Sequence */
12051184 ov2659_set_streaming(ov2659, 0);
12061185 ov2659->streaming = on;
1186
+ pm_runtime_put(&client->dev);
12071187 goto unlock;
12081188 }
12091189
1210
- ret = ov2659_set_pixel_clock(ov2659);
1190
+ ret = pm_runtime_get_sync(&client->dev);
1191
+ if (ret < 0) {
1192
+ pm_runtime_put_noidle(&client->dev);
1193
+ goto unlock;
1194
+ }
1195
+
1196
+ ret = ov2659_init(sd, 0);
1197
+ if (!ret)
1198
+ ret = ov2659_set_pixel_clock(ov2659);
12111199 if (!ret)
12121200 ret = ov2659_set_frame_size(ov2659);
12131201 if (!ret)
....@@ -1249,12 +1237,18 @@
12491237 {
12501238 struct ov2659 *ov2659 =
12511239 container_of(ctrl->handler, struct ov2659, ctrls);
1240
+ struct i2c_client *client = ov2659->client;
1241
+
1242
+ /* V4L2 controls values will be applied only when power is already up */
1243
+ if (!pm_runtime_get_if_in_use(&client->dev))
1244
+ return 0;
12521245
12531246 switch (ctrl->id) {
12541247 case V4L2_CID_TEST_PATTERN:
12551248 return ov2659_set_test_pattern(ov2659, ctrl->val);
12561249 }
12571250
1251
+ pm_runtime_put(&client->dev);
12581252 return 0;
12591253 }
12601254
....@@ -1266,6 +1260,49 @@
12661260 "Disabled",
12671261 "Vertical Color Bars",
12681262 };
1263
+
1264
+static int ov2659_power_off(struct device *dev)
1265
+{
1266
+ struct i2c_client *client = to_i2c_client(dev);
1267
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
1268
+ struct ov2659 *ov2659 = to_ov2659(sd);
1269
+
1270
+ dev_dbg(&client->dev, "%s:\n", __func__);
1271
+
1272
+ gpiod_set_value(ov2659->pwdn_gpio, 1);
1273
+
1274
+ clk_disable_unprepare(ov2659->clk);
1275
+
1276
+ return 0;
1277
+}
1278
+
1279
+static int ov2659_power_on(struct device *dev)
1280
+{
1281
+ struct i2c_client *client = to_i2c_client(dev);
1282
+ struct v4l2_subdev *sd = i2c_get_clientdata(client);
1283
+ struct ov2659 *ov2659 = to_ov2659(sd);
1284
+ int ret;
1285
+
1286
+ dev_dbg(&client->dev, "%s:\n", __func__);
1287
+
1288
+ ret = clk_prepare_enable(ov2659->clk);
1289
+ if (ret) {
1290
+ dev_err(&client->dev, "%s: failed to enable clock\n",
1291
+ __func__);
1292
+ return ret;
1293
+ }
1294
+
1295
+ gpiod_set_value(ov2659->pwdn_gpio, 0);
1296
+
1297
+ if (ov2659->resetb_gpio) {
1298
+ gpiod_set_value(ov2659->resetb_gpio, 1);
1299
+ usleep_range(500, 1000);
1300
+ gpiod_set_value(ov2659->resetb_gpio, 0);
1301
+ usleep_range(3000, 5000);
1302
+ }
1303
+
1304
+ return 0;
1305
+}
12691306
12701307 /* -----------------------------------------------------------------------------
12711308 * V4L2 subdev internal operations
....@@ -1340,13 +1377,13 @@
13401377 unsigned short id;
13411378
13421379 id = OV265X_ID(pid, ver);
1343
- if (id != OV2659_ID)
1380
+ if (id != OV2659_ID) {
13441381 dev_err(&client->dev,
13451382 "Sensor detection failed (%04X, %d)\n",
13461383 id, ret);
1347
- else {
1384
+ ret = -ENODEV;
1385
+ } else {
13481386 dev_info(&client->dev, "Found OV%04X sensor\n", id);
1349
- ret = ov2659_init(sd, 0);
13501387 }
13511388 }
13521389
....@@ -1357,8 +1394,9 @@
13571394 ov2659_get_pdata(struct i2c_client *client)
13581395 {
13591396 struct ov2659_platform_data *pdata;
1360
- struct v4l2_fwnode_endpoint *bus_cfg;
1397
+ struct v4l2_fwnode_endpoint bus_cfg = { .bus_type = 0 };
13611398 struct device_node *endpoint;
1399
+ int ret;
13621400
13631401 if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
13641402 return client->dev.platform_data;
....@@ -1367,8 +1405,9 @@
13671405 if (!endpoint)
13681406 return NULL;
13691407
1370
- bus_cfg = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(endpoint));
1371
- if (IS_ERR(bus_cfg)) {
1408
+ ret = v4l2_fwnode_endpoint_alloc_parse(of_fwnode_handle(endpoint),
1409
+ &bus_cfg);
1410
+ if (ret) {
13721411 pdata = NULL;
13731412 goto done;
13741413 }
....@@ -1377,28 +1416,26 @@
13771416 if (!pdata)
13781417 goto done;
13791418
1380
- if (!bus_cfg->nr_of_link_frequencies) {
1419
+ if (!bus_cfg.nr_of_link_frequencies) {
13811420 dev_err(&client->dev,
13821421 "link-frequencies property not found or too many\n");
13831422 pdata = NULL;
13841423 goto done;
13851424 }
13861425
1387
- pdata->link_frequency = bus_cfg->link_frequencies[0];
1426
+ pdata->link_frequency = bus_cfg.link_frequencies[0];
13881427
13891428 done:
1390
- v4l2_fwnode_endpoint_free(bus_cfg);
1429
+ v4l2_fwnode_endpoint_free(&bus_cfg);
13911430 of_node_put(endpoint);
13921431 return pdata;
13931432 }
13941433
1395
-static int ov2659_probe(struct i2c_client *client,
1396
- const struct i2c_device_id *id)
1434
+static int ov2659_probe(struct i2c_client *client)
13971435 {
13981436 const struct ov2659_platform_data *pdata = ov2659_get_pdata(client);
13991437 struct v4l2_subdev *sd;
14001438 struct ov2659 *ov2659;
1401
- struct clk *clk;
14021439 int ret;
14031440
14041441 if (!pdata) {
....@@ -1413,14 +1450,26 @@
14131450 ov2659->pdata = pdata;
14141451 ov2659->client = client;
14151452
1416
- clk = devm_clk_get(&client->dev, "xvclk");
1417
- if (IS_ERR(clk))
1418
- return PTR_ERR(clk);
1453
+ ov2659->clk = devm_clk_get(&client->dev, "xvclk");
1454
+ if (IS_ERR(ov2659->clk))
1455
+ return PTR_ERR(ov2659->clk);
14191456
1420
- ov2659->xvclk_frequency = clk_get_rate(clk);
1457
+ ov2659->xvclk_frequency = clk_get_rate(ov2659->clk);
14211458 if (ov2659->xvclk_frequency < 6000000 ||
14221459 ov2659->xvclk_frequency > 27000000)
14231460 return -EINVAL;
1461
+
1462
+ /* Optional gpio don't fail if not present */
1463
+ ov2659->pwdn_gpio = devm_gpiod_get_optional(&client->dev, "powerdown",
1464
+ GPIOD_OUT_LOW);
1465
+ if (IS_ERR(ov2659->pwdn_gpio))
1466
+ return PTR_ERR(ov2659->pwdn_gpio);
1467
+
1468
+ /* Optional gpio don't fail if not present */
1469
+ ov2659->resetb_gpio = devm_gpiod_get_optional(&client->dev, "reset",
1470
+ GPIOD_OUT_HIGH);
1471
+ if (IS_ERR(ov2659->resetb_gpio))
1472
+ return PTR_ERR(ov2659->resetb_gpio);
14241473
14251474 v4l2_ctrl_handler_init(&ov2659->ctrls, 2);
14261475 ov2659->link_frequency =
....@@ -1467,6 +1516,10 @@
14671516 ov2659->frame_size = &ov2659_framesizes[2];
14681517 ov2659->format_ctrl_regs = ov2659_formats[0].format_ctrl_regs;
14691518
1519
+ ret = ov2659_power_on(&client->dev);
1520
+ if (ret < 0)
1521
+ goto error;
1522
+
14701523 ret = ov2659_detect(sd);
14711524 if (ret < 0)
14721525 goto error;
....@@ -1480,10 +1533,15 @@
14801533
14811534 dev_info(&client->dev, "%s sensor driver registered !!\n", sd->name);
14821535
1536
+ pm_runtime_set_active(&client->dev);
1537
+ pm_runtime_enable(&client->dev);
1538
+ pm_runtime_idle(&client->dev);
1539
+
14831540 return 0;
14841541
14851542 error:
14861543 v4l2_ctrl_handler_free(&ov2659->ctrls);
1544
+ ov2659_power_off(&client->dev);
14871545 media_entity_cleanup(&sd->entity);
14881546 mutex_destroy(&ov2659->lock);
14891547 return ret;
....@@ -1499,8 +1557,17 @@
14991557 media_entity_cleanup(&sd->entity);
15001558 mutex_destroy(&ov2659->lock);
15011559
1560
+ pm_runtime_disable(&client->dev);
1561
+ if (!pm_runtime_status_suspended(&client->dev))
1562
+ ov2659_power_off(&client->dev);
1563
+ pm_runtime_set_suspended(&client->dev);
1564
+
15021565 return 0;
15031566 }
1567
+
1568
+static const struct dev_pm_ops ov2659_pm_ops = {
1569
+ SET_RUNTIME_PM_OPS(ov2659_power_off, ov2659_power_on, NULL)
1570
+};
15041571
15051572 static const struct i2c_device_id ov2659_id[] = {
15061573 { "ov2659", 0 },
....@@ -1519,9 +1586,10 @@
15191586 static struct i2c_driver ov2659_i2c_driver = {
15201587 .driver = {
15211588 .name = DRIVER_NAME,
1589
+ .pm = &ov2659_pm_ops,
15221590 .of_match_table = of_match_ptr(ov2659_of_match),
15231591 },
1524
- .probe = ov2659_probe,
1592
+ .probe_new = ov2659_probe,
15251593 .remove = ov2659_remove,
15261594 .id_table = ov2659_id,
15271595 };