1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * techpoint v4l2 driver
4 *
5 * Copyright (C) 2023 Rockchip Electronics Co., Ltd.
6 *
7 */
8
9 #include <sound/core.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <sound/soc.h>
13 #include <sound/tlv.h>
14 #include "techpoint_dev.h"
15
16 #define TECHPOINT_NAME "techpoint"
17
18 #define OF_CAMERA_PINCTRL_STATE_DEFAULT "rockchip,camera_default"
19 #define OF_CAMERA_PINCTRL_STATE_SLEEP "rockchip,camera_sleep"
20
21 #define I2S 0
22 #define DSP 1
23 #define AUDIO_FORMAT I2S
24
25 #define SAMPLE_8K 0
26 #define SAMPLE_16K 1
27 #define SAMPLE_RATE SAMPLE_8K
28
29 #define DATA_16BIT 0
30 #define DATA_8BIT 1
31 #define DATA_BIT DATA_16BIT
32
33 #define AUDIO_CHN 8
34 #define MAX_CHIPS 4
35 #define MAX_SLAVES (MAX_CHIPS - 1)
36
37 #define TECHPOINT_I2C_CHIP_ADDRESS_0 0x44
38 #define TECHPOINT_I2C_CHIP_ADDRESS_1 0x45
39
40 static int g_idx;
41 static struct techpoint *g_techpoints[MAX_CHIPS];
42
43 static const char *const techpoint_supply_names[] = {
44 "dovdd", /* Digital I/O power */
45 "avdd", /* Analog power */
46 "dvdd", /* Digital power */
47 };
48
49 #define TECHPOINT_NUM_SUPPLIES ARRAY_SIZE(techpoint_supply_names)
50
51 #define to_techpoint(sd) container_of(sd, struct techpoint, subdev)
52
techpoint_get_regulators(struct techpoint * techpoint)53 static int techpoint_get_regulators(struct techpoint *techpoint)
54 {
55 unsigned int i;
56 struct i2c_client *client = techpoint->client;
57 struct device *dev = &techpoint->client->dev;
58
59 if (!techpoint->supplies)
60 techpoint->supplies = devm_kzalloc(dev,
61 sizeof(struct
62 regulator_bulk_data) *
63 TECHPOINT_NUM_SUPPLIES,
64 GFP_KERNEL);
65
66 for (i = 0; i < TECHPOINT_NUM_SUPPLIES; i++)
67 techpoint->supplies[i].supply = techpoint_supply_names[i];
68 return devm_regulator_bulk_get(&client->dev,
69 TECHPOINT_NUM_SUPPLIES,
70 techpoint->supplies);
71 }
72
techpoint_analyze_dts(struct techpoint * techpoint)73 static int techpoint_analyze_dts(struct techpoint *techpoint)
74 {
75 int ret;
76 struct i2c_client *client = techpoint->client;
77 struct device *dev = &client->dev;
78 struct device_node *node = dev->of_node;
79 struct device_node *endpoint;
80 struct fwnode_handle *fwnode;
81 int rval;
82
83 ret = of_property_read_u32(node, RKMODULE_CAMERA_MODULE_INDEX,
84 &techpoint->module_index);
85 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_FACING,
86 &techpoint->module_facing);
87 ret |= of_property_read_string(node, RKMODULE_CAMERA_MODULE_NAME,
88 &techpoint->module_name);
89 ret |= of_property_read_string(node, RKMODULE_CAMERA_LENS_NAME,
90 &techpoint->len_name);
91 if (ret) {
92 dev_err(dev, "could not get %s!\n", RKMODULE_CAMERA_LENS_NAME);
93 return -EINVAL;
94 }
95
96 ret = of_property_read_u32(node, TECHPOINT_CAMERA_XVCLK_FREQ,
97 &techpoint->xvclk_freq_value);
98 if (ret)
99 techpoint->xvclk_freq_value = 27000000;
100
101 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
102 if (!endpoint) {
103 dev_err(dev, "Failed to get endpoint\n");
104 return -EINVAL;
105 }
106 fwnode = of_fwnode_handle(endpoint);
107 rval = fwnode_property_read_u32_array(fwnode, "data-lanes", NULL, 0);
108 if (rval <= 0) {
109 dev_err(dev, " Get mipi lane num failed!\n");
110 return -EINVAL;
111 }
112 techpoint->data_lanes = rval;
113
114 techpoint->xvclk = devm_clk_get(dev, "xvclk");
115 if (IS_ERR(techpoint->xvclk)) {
116 dev_err(dev, "Failed to get xvclk\n");
117 return -EINVAL;
118 }
119
120 techpoint->power_gpio = devm_gpiod_get(dev, "power", GPIOD_OUT_HIGH);
121 if (IS_ERR(techpoint->power_gpio))
122 dev_warn(dev, "Failed to get power-gpios\n");
123 else
124 gpiod_set_value_cansleep(techpoint->power_gpio, 1);
125
126 techpoint_get_regulators(techpoint);
127 ret =
128 regulator_bulk_enable(TECHPOINT_NUM_SUPPLIES, techpoint->supplies);
129 if (ret < 0)
130 dev_warn(dev, "Failed to enable regulators\n");
131
132 techpoint->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
133 if (IS_ERR(techpoint->reset_gpio))
134 dev_warn(dev, "Failed to get reset-gpios\n");
135 else
136 gpiod_set_value_cansleep(techpoint->reset_gpio, 0);
137
138 techpoint->pinctrl = devm_pinctrl_get(dev);
139 if (!IS_ERR(techpoint->pinctrl)) {
140 techpoint->pins_default =
141 pinctrl_lookup_state(techpoint->pinctrl,
142 OF_CAMERA_PINCTRL_STATE_DEFAULT);
143 if (IS_ERR(techpoint->pins_default))
144 dev_warn(dev, "could not get default pinstate\n");
145
146 techpoint->pins_sleep =
147 pinctrl_lookup_state(techpoint->pinctrl,
148 OF_CAMERA_PINCTRL_STATE_SLEEP);
149 if (IS_ERR(techpoint->pins_sleep))
150 dev_warn(dev, "could not get sleep pinstate\n");
151 } else {
152 dev_warn(dev, "no pinctrl\n");
153 }
154
155 return 0;
156 }
157
techpoint_initialize_controls(struct techpoint * techpoint)158 static int techpoint_initialize_controls(struct techpoint *techpoint)
159 {
160 int ret;
161 u64 pixel_rate;
162 struct v4l2_ctrl_handler *handler;
163 const struct techpoint_video_modes *mode;
164 struct device *dev = &techpoint->client->dev;
165
166 handler = &techpoint->ctrl_handler;
167 mode = techpoint->cur_video_mode;
168
169 if (techpoint->input_type == TECHPOINT_DVP_BT1120) {
170 ret = v4l2_ctrl_handler_init(handler, 1);
171 if (ret)
172 return ret;
173 handler->lock = &techpoint->mutex;
174 pixel_rate = mode->link_freq_value;
175 techpoint->pixel_rate_ctrl = v4l2_ctrl_new_std(handler, NULL,
176 V4L2_CID_PIXEL_RATE,
177 0, pixel_rate, 1,
178 pixel_rate);
179 dev_dbg(dev, "initialize pixel_rate %lld\n", pixel_rate);
180 } else if (techpoint->input_type == TECHPOINT_MIPI) {
181 ret = v4l2_ctrl_handler_init(handler, 2);
182 if (ret)
183 return ret;
184 handler->lock = &techpoint->mutex;
185 techpoint->link_freq_ctrl =
186 v4l2_ctrl_new_int_menu(handler, NULL, V4L2_CID_LINK_FREQ, 0,
187 0, &mode->link_freq_value);
188 __v4l2_ctrl_s_ctrl(techpoint->link_freq_ctrl, 0);
189 dev_dbg(dev, "initialize link_freq %lld\n",
190 mode->link_freq_value);
191
192 pixel_rate =
193 (u32) mode->link_freq_value / mode->bpp * 2 * mode->lane;
194 techpoint->pixel_rate_ctrl =
195 v4l2_ctrl_new_std(handler, NULL, V4L2_CID_PIXEL_RATE, 0,
196 pixel_rate, 1, pixel_rate);
197 dev_dbg(dev, "initialize pixel_rate %lld\n", pixel_rate);
198 }
199
200 if (handler->error) {
201 ret = handler->error;
202 dev_err(dev, "Failed to init controls(%d)\n", ret);
203 goto err_free_handler;
204 }
205
206 techpoint->subdev.ctrl_handler = handler;
207
208 return 0;
209
210 err_free_handler:
211 v4l2_ctrl_handler_free(handler);
212
213 return ret;
214 }
215
__techpoint_power_on(struct techpoint * techpoint)216 static int __techpoint_power_on(struct techpoint *techpoint)
217 {
218 int ret;
219 struct device *dev = &techpoint->client->dev;
220
221 if (!IS_ERR_OR_NULL(techpoint->pins_default)) {
222 ret = pinctrl_select_state(techpoint->pinctrl,
223 techpoint->pins_default);
224 if (ret < 0)
225 dev_err(dev, "could not set pins. ret=%d\n", ret);
226 }
227
228 if (!IS_ERR(techpoint->power_gpio)) {
229 gpiod_set_value_cansleep(techpoint->power_gpio, 1);
230 usleep_range(25 * 1000, 30 * 1000);
231 }
232
233 usleep_range(1500, 2000);
234
235 if (!IS_ERR(techpoint->xvclk)) {
236 ret =
237 clk_set_rate(techpoint->xvclk, techpoint->xvclk_freq_value);
238 if (ret < 0)
239 dev_warn(dev, "Failed to set xvclk rate\n");
240 if (clk_get_rate(techpoint->xvclk) !=
241 techpoint->xvclk_freq_value)
242 dev_warn(dev, "xvclk mismatched\n");
243 ret = clk_prepare_enable(techpoint->xvclk);
244 if (ret < 0) {
245 dev_err(dev, "Failed to enable xvclk\n");
246 goto err_clk;
247 }
248 }
249
250 if (!IS_ERR(techpoint->reset_gpio)) {
251 gpiod_set_value_cansleep(techpoint->reset_gpio, 0);
252 usleep_range(10 * 1000, 20 * 1000);
253 gpiod_set_value_cansleep(techpoint->reset_gpio, 1);
254 usleep_range(10 * 1000, 20 * 1000);
255 gpiod_set_value_cansleep(techpoint->reset_gpio, 0);
256 }
257
258 usleep_range(10 * 1000, 20 * 1000);
259
260 return 0;
261
262 err_clk:
263 if (!IS_ERR(techpoint->reset_gpio))
264 gpiod_set_value_cansleep(techpoint->reset_gpio, 0);
265
266 if (!IS_ERR_OR_NULL(techpoint->pins_sleep))
267 pinctrl_select_state(techpoint->pinctrl, techpoint->pins_sleep);
268
269 if (!IS_ERR(techpoint->power_gpio))
270 gpiod_set_value_cansleep(techpoint->power_gpio, 0);
271
272 return ret;
273 }
274
__techpoint_power_off(struct techpoint * techpoint)275 static void __techpoint_power_off(struct techpoint *techpoint)
276 {
277 int ret;
278
279 #if TECHPOINT_SHARING_POWER
280 return;
281 #endif
282
283 if (!IS_ERR(techpoint->reset_gpio))
284 gpiod_set_value_cansleep(techpoint->reset_gpio, 1);
285
286 if (IS_ERR(techpoint->xvclk))
287 clk_disable_unprepare(techpoint->xvclk);
288
289 if (!IS_ERR_OR_NULL(techpoint->pins_sleep)) {
290 ret = pinctrl_select_state(techpoint->pinctrl,
291 techpoint->pins_sleep);
292 if (ret < 0)
293 dev_err(&techpoint->client->dev, "could not set pins\n");
294 }
295
296 if (!IS_ERR(techpoint->power_gpio))
297 gpiod_set_value_cansleep(techpoint->power_gpio, 0);
298 }
299
techpoint_runtime_resume(struct device * dev)300 static int techpoint_runtime_resume(struct device *dev)
301 {
302 struct i2c_client *client = to_i2c_client(dev);
303 struct v4l2_subdev *sd = i2c_get_clientdata(client);
304 struct techpoint *techpoint = to_techpoint(sd);
305
306 return __techpoint_power_on(techpoint);
307 }
308
techpoint_runtime_suspend(struct device * dev)309 static int techpoint_runtime_suspend(struct device *dev)
310 {
311 struct i2c_client *client = to_i2c_client(dev);
312 struct v4l2_subdev *sd = i2c_get_clientdata(client);
313 struct techpoint *techpoint = to_techpoint(sd);
314
315 __techpoint_power_off(techpoint);
316
317 return 0;
318 }
319
techpoint_power(struct v4l2_subdev * sd,int on)320 static int techpoint_power(struct v4l2_subdev *sd, int on)
321 {
322 struct techpoint *techpoint = to_techpoint(sd);
323 struct i2c_client *client = techpoint->client;
324 int ret = 0;
325
326 mutex_lock(&techpoint->mutex);
327
328 /* If the power state is not modified - no work to do. */
329 if (techpoint->power_on == !!on)
330 goto exit;
331
332 dev_dbg(&client->dev, "%s: on %d\n", __func__, on);
333
334 if (on) {
335 ret = pm_runtime_get_sync(&client->dev);
336 if (ret < 0) {
337 pm_runtime_put_noidle(&client->dev);
338 goto exit;
339 }
340 techpoint->power_on = true;
341 } else {
342 pm_runtime_put(&client->dev);
343 techpoint->power_on = false;
344 }
345
346 exit:
347 mutex_unlock(&techpoint->mutex);
348
349 return ret;
350 }
351
techpoint_get_reso_dist(struct techpoint_video_modes * mode,struct v4l2_mbus_framefmt * framefmt)352 static int techpoint_get_reso_dist(struct techpoint_video_modes *mode,
353 struct v4l2_mbus_framefmt *framefmt)
354 {
355 return abs(mode->width - framefmt->width) +
356 abs(mode->height - framefmt->height);
357 }
358
359 static struct techpoint_video_modes *
techpoint_find_best_fit(struct techpoint * techpoint,struct v4l2_subdev_format * fmt)360 techpoint_find_best_fit(struct techpoint *techpoint,
361 struct v4l2_subdev_format *fmt)
362 {
363 struct v4l2_mbus_framefmt *framefmt = &fmt->format;
364 int dist;
365 int cur_best_fit = 0;
366 int cur_best_fit_dist = -1;
367 unsigned int i;
368
369 for (i = 0; i < techpoint->video_modes_num; i++) {
370 dist =
371 techpoint_get_reso_dist(&techpoint->video_modes[i],
372 framefmt);
373 if ((cur_best_fit_dist == -1 || dist <= cur_best_fit_dist) &&
374 techpoint->video_modes[i].bus_fmt == framefmt->code) {
375 cur_best_fit_dist = dist;
376 cur_best_fit = i;
377 }
378 }
379
380 return &techpoint->video_modes[cur_best_fit];
381 }
382
techpoint_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)383 static int techpoint_set_fmt(struct v4l2_subdev *sd,
384 struct v4l2_subdev_pad_config *cfg,
385 struct v4l2_subdev_format *fmt)
386 {
387 struct techpoint *techpoint = to_techpoint(sd);
388 struct techpoint_video_modes *mode;
389
390 mutex_lock(&techpoint->mutex);
391
392 mode = techpoint_find_best_fit(techpoint, fmt);
393 techpoint->cur_video_mode = mode;
394 fmt->format.code = mode->bus_fmt;
395 fmt->format.width = mode->width;
396 fmt->format.height = mode->height;
397 fmt->format.field = V4L2_FIELD_NONE;
398 fmt->format.colorspace = V4L2_COLORSPACE_SRGB;
399
400 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
401 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
402 *v4l2_subdev_get_try_format(sd, cfg, fmt->pad) = fmt->format;
403 #else
404 mutex_unlock(&techpoint->mutex);
405 return -ENOTTY;
406 #endif
407 } else {
408 if (techpoint->streaming) {
409 mutex_unlock(&techpoint->mutex);
410 return -EBUSY;
411 }
412 }
413
414 mutex_unlock(&techpoint->mutex);
415 return 0;
416 }
417
techpoint_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_format * fmt)418 static int techpoint_get_fmt(struct v4l2_subdev *sd,
419 struct v4l2_subdev_pad_config *cfg,
420 struct v4l2_subdev_format *fmt)
421 {
422 struct techpoint *techpoint = to_techpoint(sd);
423 struct i2c_client *client = techpoint->client;
424 const struct techpoint_video_modes *mode = techpoint->cur_video_mode;
425
426 mutex_lock(&techpoint->mutex);
427 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
428 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
429 fmt->format = *v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
430 #else
431 mutex_unlock(&techpoint->mutex);
432 return -ENOTTY;
433 #endif
434 } else {
435 fmt->format.width = mode->width;
436 fmt->format.height = mode->height;
437 fmt->format.code = mode->bus_fmt;
438 fmt->format.field = V4L2_FIELD_NONE;
439 if (fmt->pad < PAD_MAX) {
440 if (mode->channel_reso[fmt->pad] == TECHPOINT_S_RESO_SD)
441 fmt->format.field = V4L2_FIELD_INTERLACED;
442 fmt->reserved[0] = mode->vc[fmt->pad];
443 }
444 }
445 mutex_unlock(&techpoint->mutex);
446
447 dev_dbg(&client->dev, "%s: %x %dx%d\n",
448 __func__, fmt->format.code,
449 fmt->format.width, fmt->format.height);
450
451 return 0;
452 }
453
techpoint_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_mbus_code_enum * code)454 static int techpoint_enum_mbus_code(struct v4l2_subdev *sd,
455 struct v4l2_subdev_pad_config *cfg,
456 struct v4l2_subdev_mbus_code_enum *code)
457 {
458 struct techpoint *techpoint = to_techpoint(sd);
459
460 if (code->index != 0)
461 return -EINVAL;
462 code->code = techpoint->cur_video_mode->bus_fmt;
463
464 return 0;
465 }
466
techpoint_enum_frame_sizes(struct v4l2_subdev * sd,struct v4l2_subdev_pad_config * cfg,struct v4l2_subdev_frame_size_enum * fse)467 static int techpoint_enum_frame_sizes(struct v4l2_subdev *sd,
468 struct v4l2_subdev_pad_config *cfg,
469 struct v4l2_subdev_frame_size_enum *fse)
470 {
471 struct techpoint *techpoint = to_techpoint(sd);
472
473 if (fse->index >= techpoint->video_modes_num)
474 return -EINVAL;
475
476 if (fse->code != techpoint->video_modes[fse->index].bus_fmt)
477 return -EINVAL;
478
479 fse->min_width = techpoint->video_modes[fse->index].width;
480 fse->max_width = techpoint->video_modes[fse->index].width;
481 fse->max_height = techpoint->video_modes[fse->index].height;
482 fse->min_height = techpoint->video_modes[fse->index].height;
483
484 return 0;
485 }
486
techpoint_g_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_frame_interval * fi)487 static int techpoint_g_frame_interval(struct v4l2_subdev *sd,
488 struct v4l2_subdev_frame_interval *fi)
489 {
490 struct techpoint *techpoint = to_techpoint(sd);
491
492 mutex_lock(&techpoint->mutex);
493 fi->interval = techpoint->cur_video_mode->max_fps;
494 mutex_unlock(&techpoint->mutex);
495
496 return 0;
497 }
498
techpoint_g_mbus_config(struct v4l2_subdev * sd,unsigned int pad_id,struct v4l2_mbus_config * cfg)499 static int techpoint_g_mbus_config(struct v4l2_subdev *sd,
500 unsigned int pad_id,
501 struct v4l2_mbus_config *cfg)
502 {
503 struct techpoint *techpoint = to_techpoint(sd);
504
505 if (techpoint->input_type == TECHPOINT_DVP_BT1120) {
506 cfg->type = V4L2_MBUS_BT656;
507 cfg->flags = RKMODULE_CAMERA_BT656_CHANNELS |
508 V4L2_MBUS_PCLK_SAMPLE_RISING |
509 V4L2_MBUS_PCLK_SAMPLE_FALLING;
510 } else if (techpoint->input_type == TECHPOINT_MIPI) {
511 cfg->type = V4L2_MBUS_CSI2_DPHY;
512 if (techpoint->data_lanes == 4) {
513 cfg->flags = V4L2_MBUS_CSI2_4_LANE | V4L2_MBUS_CSI2_CHANNELS;
514 } else if (techpoint->data_lanes == 2) {
515 cfg->flags = V4L2_MBUS_CSI2_2_LANE |
516 V4L2_MBUS_CSI2_CHANNEL_0 |
517 V4L2_MBUS_CSI2_CHANNEL_1;
518 }
519 }
520
521 return 0;
522 }
523
techpoint_querystd(struct v4l2_subdev * sd,v4l2_std_id * std)524 static int techpoint_querystd(struct v4l2_subdev *sd, v4l2_std_id *std)
525 {
526 struct techpoint *techpoint = to_techpoint(sd);
527
528 if (techpoint->input_type == TECHPOINT_DVP_BT1120)
529 *std = V4L2_STD_ATSC;
530
531 return 0;
532 }
533
techpoint_get_module_inf(struct techpoint * techpoint,struct rkmodule_inf * inf)534 static __maybe_unused void techpoint_get_module_inf(struct techpoint *techpoint,
535 struct rkmodule_inf *inf)
536 {
537 memset(inf, 0, sizeof(*inf));
538 strscpy(inf->base.sensor, TECHPOINT_NAME, sizeof(inf->base.sensor));
539 strscpy(inf->base.module, techpoint->module_name,
540 sizeof(inf->base.module));
541 strscpy(inf->base.lens, techpoint->len_name, sizeof(inf->base.lens));
542 }
543
544 static __maybe_unused void
techpoint_get_bt656_module_inf(struct techpoint * techpoint,struct rkmodule_bt656_mbus_info * inf)545 techpoint_get_bt656_module_inf(struct techpoint *techpoint,
546 struct rkmodule_bt656_mbus_info *inf)
547 {
548 memset(inf, 0, sizeof(*inf));
549 if (techpoint->input_type == TECHPOINT_DVP_BT1120) {
550 inf->id_en_bits = RKMODULE_CAMERA_BT656_ID_EN_BITS_2;
551 inf->flags = RKMODULE_CAMERA_BT656_PARSE_ID_LSB |
552 RKMODULE_CAMERA_BT656_CHANNELS;
553 }
554 }
555
techpoint_get_vicap_rst_inf(struct techpoint * techpoint,struct rkmodule_vicap_reset_info * rst_info)556 static void techpoint_get_vicap_rst_inf(struct techpoint *techpoint,
557 struct rkmodule_vicap_reset_info *rst_info)
558 {
559 rst_info->is_reset = techpoint->do_reset;
560 rst_info->src = RKCIF_RESET_SRC_ERR_HOTPLUG;
561 }
562
techpoint_set_vicap_rst_inf(struct techpoint * techpoint,struct rkmodule_vicap_reset_info * rst_info)563 static void techpoint_set_vicap_rst_inf(struct techpoint *techpoint,
564 struct rkmodule_vicap_reset_info *rst_info)
565 {
566 techpoint->do_reset = rst_info->is_reset;
567 }
568
techpoint_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)569 static long techpoint_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
570 {
571 struct techpoint *techpoint = to_techpoint(sd);
572 long ret = 0;
573
574 switch (cmd) {
575 case RKMODULE_GET_MODULE_INFO:
576 techpoint_get_module_inf(techpoint, (struct rkmodule_inf *)arg);
577 break;
578 case RKMODULE_GET_BT656_MBUS_INFO:
579 techpoint_get_bt656_module_inf(techpoint,
580 (struct rkmodule_bt656_mbus_info
581 *)arg);
582 break;
583 case RKMODULE_GET_VC_FMT_INFO:
584 if (!techpoint->streaming)
585 techpoint_get_vc_fmt_inf(techpoint,
586 (struct rkmodule_vc_fmt_info *)
587 arg);
588 else
589 __techpoint_get_vc_fmt_inf(techpoint,
590 (struct rkmodule_vc_fmt_info
591 *)arg);
592 break;
593 case RKMODULE_GET_VC_HOTPLUG_INFO:
594 techpoint_get_vc_hotplug_inf(techpoint,
595 (struct rkmodule_vc_hotplug_info *)
596 arg);
597 break;
598 case RKMODULE_GET_START_STREAM_SEQ:
599 *(int *)arg = RKMODULE_START_STREAM_FRONT;
600 break;
601 case RKMODULE_GET_VICAP_RST_INFO:
602 techpoint_get_vicap_rst_inf(techpoint,
603 (struct rkmodule_vicap_reset_info *)
604 arg);
605 break;
606 case RKMODULE_SET_VICAP_RST_INFO:
607 techpoint_set_vicap_rst_inf(techpoint,
608 (struct rkmodule_vicap_reset_info *)
609 arg);
610 break;
611 case RKMODULE_SET_QUICK_STREAM:
612 techpoint_set_quick_stream(techpoint, *((u32 *)arg));
613 break;
614 default:
615 ret = -ENOTTY;
616 break;
617 }
618
619 return ret;
620 }
621
622 #ifdef CONFIG_COMPAT
techpoint_compat_ioctl32(struct v4l2_subdev * sd,unsigned int cmd,unsigned long arg)623 static long techpoint_compat_ioctl32(struct v4l2_subdev *sd,
624 unsigned int cmd, unsigned long arg)
625 {
626 void __user *up = compat_ptr(arg);
627 struct rkmodule_inf *inf;
628 struct rkmodule_bt656_mbus_info *bt565_inf;
629 struct rkmodule_awb_cfg *cfg;
630 struct rkmodule_vc_fmt_info *vc_fmt_inf;
631 struct rkmodule_vc_hotplug_info *vc_hp_inf;
632 struct rkmodule_vicap_reset_info *vicap_rst_inf;
633 int *stream_seq;
634 u32 stream;
635 long ret = 0;
636
637 switch (cmd) {
638 case RKMODULE_GET_MODULE_INFO:
639 inf = kzalloc(sizeof(*inf), GFP_KERNEL);
640 if (!inf) {
641 ret = -ENOMEM;
642 return ret;
643 }
644
645 ret = techpoint_ioctl(sd, cmd, inf);
646 if (!ret) {
647 ret = copy_to_user(up, inf, sizeof(*inf));
648 if (ret)
649 ret = -EFAULT;
650 }
651 kfree(inf);
652 break;
653 case RKMODULE_AWB_CFG:
654 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
655 if (!cfg) {
656 ret = -ENOMEM;
657 return ret;
658 }
659
660 if (copy_from_user(cfg, up, sizeof(*cfg))) {
661 kfree(cfg);
662 return -EFAULT;
663 }
664 ret = techpoint_ioctl(sd, cmd, cfg);
665 kfree(cfg);
666 break;
667 case RKMODULE_GET_VC_FMT_INFO:
668 vc_fmt_inf = kzalloc(sizeof(*vc_fmt_inf), GFP_KERNEL);
669 if (!vc_fmt_inf) {
670 ret = -ENOMEM;
671 return ret;
672 }
673
674 ret = techpoint_ioctl(sd, cmd, vc_fmt_inf);
675 if (!ret) {
676 ret = copy_to_user(up, vc_fmt_inf, sizeof(*vc_fmt_inf));
677 if (ret)
678 ret = -EFAULT;
679 }
680 kfree(vc_fmt_inf);
681 break;
682 case RKMODULE_GET_VC_HOTPLUG_INFO:
683 vc_hp_inf = kzalloc(sizeof(*vc_hp_inf), GFP_KERNEL);
684 if (!vc_hp_inf) {
685 ret = -ENOMEM;
686 return ret;
687 }
688
689 ret = techpoint_ioctl(sd, cmd, vc_hp_inf);
690 if (!ret) {
691 ret = copy_to_user(up, vc_hp_inf, sizeof(*vc_hp_inf));
692 if (ret)
693 ret = -EFAULT;
694 }
695 kfree(vc_hp_inf);
696 break;
697 case RKMODULE_GET_BT656_MBUS_INFO:
698 bt565_inf = kzalloc(sizeof(*bt565_inf), GFP_KERNEL);
699 if (!bt565_inf) {
700 ret = -ENOMEM;
701 return ret;
702 }
703
704 ret = techpoint_ioctl(sd, cmd, bt565_inf);
705 if (!ret) {
706 ret = copy_to_user(up, bt565_inf, sizeof(*bt565_inf));
707 if (ret)
708 ret = -EFAULT;
709 }
710 kfree(bt565_inf);
711 break;
712 case RKMODULE_GET_VICAP_RST_INFO:
713 vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
714 if (!vicap_rst_inf) {
715 ret = -ENOMEM;
716 return ret;
717 }
718
719 ret = techpoint_ioctl(sd, cmd, vicap_rst_inf);
720 if (!ret) {
721 ret = copy_to_user(up, vicap_rst_inf,
722 sizeof(*vicap_rst_inf));
723 if (ret)
724 ret = -EFAULT;
725 }
726 kfree(vicap_rst_inf);
727 break;
728 case RKMODULE_SET_VICAP_RST_INFO:
729 vicap_rst_inf = kzalloc(sizeof(*vicap_rst_inf), GFP_KERNEL);
730 if (!vicap_rst_inf) {
731 ret = -ENOMEM;
732 return ret;
733 }
734
735 if (copy_from_user(vicap_rst_inf, up, sizeof(*vicap_rst_inf))) {
736 kfree(vicap_rst_inf);
737 return -EFAULT;
738 }
739 ret = techpoint_ioctl(sd, cmd, vicap_rst_inf);
740 kfree(vicap_rst_inf);
741 break;
742 case RKMODULE_GET_START_STREAM_SEQ:
743 stream_seq = kzalloc(sizeof(*stream_seq), GFP_KERNEL);
744 if (!stream_seq) {
745 ret = -ENOMEM;
746 return ret;
747 }
748
749 ret = techpoint_ioctl(sd, cmd, stream_seq);
750 if (!ret) {
751 ret = copy_to_user(up, stream_seq, sizeof(*stream_seq));
752 if (ret)
753 return -EFAULT;
754 }
755 kfree(stream_seq);
756 break;
757 case RKMODULE_SET_QUICK_STREAM:
758 if (copy_from_user(&stream, up, sizeof(u32)))
759 return -EFAULT;
760 ret = techpoint_ioctl(sd, cmd, &stream);
761 if (ret)
762 ret = -EFAULT;
763 break;
764 default:
765 ret = -ENOIOCTLCMD;
766 break;
767 }
768
769 return ret;
770 }
771 #endif
772
__techpoint_start_stream(struct techpoint * techpoint)773 static __maybe_unused int __techpoint_start_stream(struct techpoint *techpoint)
774 {
775 techpoint_start_video_stream(techpoint);
776 return 0;
777 }
778
__techpoint_stop_stream(struct techpoint * techpoint)779 static __maybe_unused int __techpoint_stop_stream(struct techpoint *techpoint)
780 {
781 techpoint_stop_video_stream(techpoint);
782 return 0;
783 }
784
techpoint_stream(struct v4l2_subdev * sd,int on)785 static int techpoint_stream(struct v4l2_subdev *sd, int on)
786 {
787 struct techpoint *techpoint = to_techpoint(sd);
788 struct i2c_client *client = techpoint->client;
789
790 dev_dbg(&client->dev, "s_stream: %d. %dx%d\n", on,
791 techpoint->cur_video_mode->width,
792 techpoint->cur_video_mode->height);
793
794 mutex_lock(&techpoint->mutex);
795 on = !!on;
796 if (techpoint->streaming == on)
797 goto unlock;
798
799 if (on)
800 __techpoint_start_stream(techpoint);
801 else
802 __techpoint_stop_stream(techpoint);
803
804 techpoint->streaming = on;
805
806 unlock:
807 mutex_unlock(&techpoint->mutex);
808 return 0;
809 }
810
811 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
techpoint_open(struct v4l2_subdev * sd,struct v4l2_subdev_fh * fh)812 static int techpoint_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
813 {
814 return 0;
815 }
816 #endif
817
818 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
819 static const struct v4l2_subdev_internal_ops techpoint_internal_ops = {
820 .open = techpoint_open,
821 };
822 #endif
823
824 static const struct v4l2_subdev_video_ops techpoint_video_ops = {
825 .s_stream = techpoint_stream,
826 .g_frame_interval = techpoint_g_frame_interval,
827 .querystd = techpoint_querystd,
828 };
829
830 static const struct v4l2_subdev_pad_ops techpoint_subdev_pad_ops = {
831 .enum_mbus_code = techpoint_enum_mbus_code,
832 .enum_frame_size = techpoint_enum_frame_sizes,
833 .get_fmt = techpoint_get_fmt,
834 .set_fmt = techpoint_set_fmt,
835 .get_mbus_config = techpoint_g_mbus_config,
836 };
837
838 static const struct v4l2_subdev_core_ops techpoint_core_ops = {
839 .s_power = techpoint_power,
840 .ioctl = techpoint_ioctl,
841 #ifdef CONFIG_COMPAT
842 .compat_ioctl32 = techpoint_compat_ioctl32,
843 #endif
844 };
845
846 static const struct v4l2_subdev_ops techpoint_subdev_ops = {
847 .core = &techpoint_core_ops,
848 .video = &techpoint_video_ops,
849 .pad = &techpoint_subdev_pad_ops,
850 };
851
852 static const struct dev_pm_ops techpoint_pm_ops = {
853 SET_RUNTIME_PM_OPS(techpoint_runtime_suspend,
854 techpoint_runtime_resume, NULL)
855 };
856
857 #if IS_ENABLED(CONFIG_OF)
858 static const struct of_device_id techpoint_of_match[] = {
859 { .compatible = "techpoint,tp2855" },
860 { .compatible = "techpoint,tp2815" },
861 { .compatible = "techpoint,tp9930" },
862 { .compatible = "techpoint,tp9950" },
863 { },
864 };
865
866 MODULE_DEVICE_TABLE(of, techpoint_of_match);
867 #endif
868
869 static const struct i2c_device_id techpoint_match_id[] = {
870 { "techpoint", 0 },
871 { },
872 };
873
874 static int techpoint_9930_audio_init(struct techpoint *techpoint);
875
876 static struct snd_soc_dai_driver techpoint_audio_dai = {
877 .name = "techpoint",
878 .playback = {
879 .stream_name = "Playback",
880 .channels_min = 1,
881 .channels_max = 16,
882 .rates = SNDRV_PCM_RATE_8000_384000,
883 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE),
884 },
885 .capture = {
886 .stream_name = "Capture",
887 .channels_min = 1,
888 .channels_max = 16,
889 .rates = SNDRV_PCM_RATE_8000_384000,
890 .formats = (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE),
891 },
892 };
893
i2c_rdwr_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)894 static ssize_t i2c_rdwr_store(struct device *dev,
895 struct device_attribute *attr,
896 const char *buf, size_t count)
897 {
898 struct techpoint *techpoint =
899 container_of(dev, struct techpoint, dev);
900 unsigned char op_type;
901 unsigned int reg, v;
902 int ret;
903
904 ret = sscanf(buf, "%c %x %x", &op_type, ®, &v);
905 if (ret != 3) {
906 dev_err(&techpoint->client->dev, "%s sscanf failed: %d\n",
907 __func__, ret);
908 return -EFAULT;
909 }
910
911 if (op_type == 'r')
912 techpoint_read_reg(techpoint->client, reg, (unsigned char *)&v);
913 else if (op_type == 'w')
914 techpoint_write_reg(techpoint->client, reg, v);
915 else if (op_type == 'd')
916 techpoint_9930_audio_init(techpoint);
917
918 return count;
919 }
920
921 static const struct device_attribute techpoint_attrs[] = {
922 __ATTR_WO(i2c_rdwr),
923 };
924
techpoint_codec_probe(struct snd_soc_component * component)925 static int techpoint_codec_probe(struct snd_soc_component *component)
926 {
927 return 0;
928 }
929
techpoint_codec_remove(struct snd_soc_component * component)930 static void techpoint_codec_remove(struct snd_soc_component *component)
931 {
932 }
933
934 static const struct snd_soc_component_driver techpoint_codec_driver = {
935 .probe = techpoint_codec_probe,
936 .remove = techpoint_codec_remove,
937 .idle_bias_on = 1,
938 .use_pmdown_time = 1,
939 .endianness = 1,
940 .non_legacy_dai_naming = 1,
941 };
942
tp2833_audio_config_rmpos(struct i2c_client * client,unsigned int chip,unsigned int format,unsigned int chn_num)943 static int tp2833_audio_config_rmpos(struct i2c_client *client,
944 unsigned int chip, unsigned int format, unsigned int chn_num)
945 {
946 int i = 0;
947 unsigned char v;
948
949 /* clear first */
950 for (i = 0; i < 20; i++)
951 techpoint_write_reg(client, i, 0x00);
952
953 switch (chn_num) {
954
955 case 2:
956 if (format == DSP) {
957 techpoint_write_reg(client, 0x0, 1);
958 techpoint_write_reg(client, 0x1, 2);
959 } else {
960 techpoint_write_reg(client, 0x0, 1);
961 techpoint_write_reg(client, 0x8, 2);
962 }
963 break;
964
965 case 4:
966 if (format == DSP) {
967 techpoint_write_reg(client, 0x0, 1);
968 techpoint_write_reg(client, 0x1, 2);
969 techpoint_write_reg(client, 0x2, 3);
970 techpoint_write_reg(client, 0x3, 4);
971 } else {
972 techpoint_write_reg(client, 0x0, 1);
973 techpoint_write_reg(client, 0x1, 3);
974 techpoint_write_reg(client, 0x8, 2);
975 techpoint_write_reg(client, 0x9, 4);
976 }
977 break;
978
979 case 8:
980 if (chip % 4 == 0) {
981 if (format == DSP) {
982 techpoint_write_reg(client, 0x0, 1);
983 techpoint_write_reg(client, 0x1, 2);
984 techpoint_write_reg(client, 0x2, 3);
985 techpoint_write_reg(client, 0x3, 4);
986 techpoint_write_reg(client, 0x4, 5);
987 techpoint_write_reg(client, 0x5, 6);
988 techpoint_write_reg(client, 0x6, 7);
989 techpoint_write_reg(client, 0x7, 8);
990 } else {
991 techpoint_write_reg(client, 0x0, 1);
992 techpoint_write_reg(client, 0x1, 2);
993 techpoint_write_reg(client, 0x2, 3);
994 techpoint_write_reg(client, 0x3, 4);
995 techpoint_write_reg(client, 0x8, 5);
996 techpoint_write_reg(client, 0x9, 6);
997 techpoint_write_reg(client, 0xa, 7);
998 techpoint_write_reg(client, 0xb, 8);
999 }
1000 } else if (chip % 4 == 1) {
1001 if (format == DSP) {
1002 techpoint_write_reg(client, 0x0, 0);
1003 techpoint_write_reg(client, 0x1, 0);
1004 techpoint_write_reg(client, 0x2, 0);
1005 techpoint_write_reg(client, 0x3, 0);
1006 techpoint_write_reg(client, 0x4, 1);
1007 techpoint_write_reg(client, 0x5, 2);
1008 techpoint_write_reg(client, 0x6, 3);
1009 techpoint_write_reg(client, 0x7, 4);
1010 } else {
1011 techpoint_write_reg(client, 0x0, 0);
1012 techpoint_write_reg(client, 0x1, 0);
1013 techpoint_write_reg(client, 0x2, 1);
1014 techpoint_write_reg(client, 0x3, 2);
1015 techpoint_write_reg(client, 0x8, 0);
1016 techpoint_write_reg(client, 0x9, 0);
1017 techpoint_write_reg(client, 0xa, 3);
1018 techpoint_write_reg(client, 0xb, 4);
1019 techpoint_read_reg(client, 0x3, &v);
1020 }
1021 }
1022 break;
1023
1024 case 16:
1025 if (chip % 4 == 0) {
1026 for (i = 0; i < 16; i++)
1027 techpoint_write_reg(client, i, i+1);
1028 } else if (chip % 4 == 1) {
1029 for (i = 4; i < 16; i++)
1030 techpoint_write_reg(client, i, i+1 - 4);
1031 } else if (chip % 4 == 2) {
1032 for (i = 8; i < 16; i++)
1033 techpoint_write_reg(client, i, i+1 - 8);
1034 } else {
1035 for (i = 12; i < 16; i++)
1036 techpoint_write_reg(client, i, i+1 - 12);
1037 }
1038 break;
1039
1040 case 20:
1041 for (i = 0; i < 20; i++)
1042 techpoint_write_reg(client, i, i+1);
1043 break;
1044
1045 default:
1046 for (i = 0; i < 20; i++)
1047 techpoint_write_reg(client, i, i+1);
1048 break;
1049 }
1050
1051 mdelay(10);
1052 return 0;
1053 }
1054
techpoint_2855_audio_init(struct techpoint * techpoint)1055 static int techpoint_2855_audio_init(struct techpoint *techpoint)
1056 {
1057 struct i2c_client *client = techpoint->client;
1058
1059 unsigned char bank;
1060 unsigned char chip_id_h = 0xFF, chip_id_l = 0xFF;
1061
1062 techpoint_read_reg(client, CHIP_ID_H_REG, &chip_id_h);
1063 techpoint_read_reg(client, CHIP_ID_L_REG, &chip_id_l);
1064
1065 techpoint_read_reg(client, 0x40, &bank);
1066 techpoint_write_reg(client, 0x40, 0x40);
1067
1068 tp2833_audio_config_rmpos(client, 0, AUDIO_FORMAT, AUDIO_CHN);
1069
1070 techpoint_write_reg(client, 0x17, 0x00|(DATA_BIT<<2));
1071 techpoint_write_reg(client, 0x1B, 0x01|(DATA_BIT<<6));
1072
1073 #if (AUDIO_CHN == 20)
1074 techpoint_write_reg(client, 0x18, 0x90|(SAMPLE_RATE));
1075 #else
1076 techpoint_write_reg(client, 0x18, 0x80|(SAMPLE_RATE));
1077 #endif
1078
1079 #if (AUDIO_CHN >= 8)
1080 techpoint_write_reg(client, 0x19, 0x1F);
1081 #else
1082 techpoint_write_reg(client, 0x19, 0x0F);
1083 #endif
1084
1085 techpoint_write_reg(client, 0x1A, 0x15);
1086
1087 techpoint_write_reg(client, 0x37, 0x20);
1088 techpoint_write_reg(client, 0x38, 0x38);
1089 techpoint_write_reg(client, 0x3E, 0x00);
1090
1091 /* audio reset */
1092 techpoint_write_reg(client, 0x3d, 0x01);
1093
1094 techpoint_write_reg(client, 0x40, bank);
1095
1096 return 0;
1097 }
1098
techpoint_9930_audio_init(struct techpoint * techpoint)1099 static int techpoint_9930_audio_init(struct techpoint *techpoint)
1100 {
1101 struct i2c_client *client = techpoint->client;
1102
1103 unsigned char bank;
1104 unsigned char chip_id_h = 0xFF;
1105 unsigned char chip_id_l = 0xFF;
1106
1107 techpoint_read_reg(client, CHIP_ID_H_REG, &chip_id_h);
1108 techpoint_read_reg(client, CHIP_ID_L_REG, &chip_id_l);
1109
1110 techpoint_read_reg(client, 0x40, &bank);
1111 techpoint_write_reg(client, 0x40, 0x40);
1112
1113 tp2833_audio_config_rmpos(client, 1, AUDIO_FORMAT, AUDIO_CHN);
1114
1115 techpoint_write_reg(client, 0x17, 0x00|(DATA_BIT<<2));
1116 techpoint_write_reg(client, 0x1B, 0x01|(DATA_BIT<<6));
1117
1118 #if (AUDIO_CHN == 20)
1119 techpoint_write_reg(client, 0x18, 0x90|(SAMPLE_RATE));
1120 #else
1121 techpoint_write_reg(client, 0x18, 0x80|(SAMPLE_RATE));
1122 #endif
1123
1124 #if (AUDIO_CHN >= 8)
1125 techpoint_write_reg(client, 0x19, 0x1F);
1126 #else
1127 techpoint_write_reg(client, 0x19, 0x0F);
1128 #endif
1129
1130 techpoint_write_reg(client, 0x1A, 0x15);
1131 techpoint_write_reg(client, 0x37, 0x20);
1132 techpoint_write_reg(client, 0x38, 0x38);
1133 techpoint_write_reg(client, 0x3E, 0x00);
1134
1135 /* reset audio */
1136 techpoint_write_reg(client, 0x3d, 0x01);
1137
1138 techpoint_write_reg(client, 0x40, bank);
1139
1140 return 0;
1141 }
1142
techpoint_audio_init(struct techpoint * techpoint)1143 static int techpoint_audio_init(struct techpoint *techpoint)
1144 {
1145 if (techpoint)
1146 techpoint_2855_audio_init(techpoint);
1147
1148 if (techpoint && techpoint->audio_in)
1149 techpoint_9930_audio_init(techpoint->audio_in->slave_tp[0]);
1150
1151 return 0;
1152 }
1153
techpoint_audio_dt_parse(struct techpoint * techpoint)1154 static int techpoint_audio_dt_parse(struct techpoint *techpoint)
1155 {
1156 struct device *dev = &techpoint->client->dev;
1157 struct device_node *node = dev->of_node;
1158 const char *str;
1159 u32 v;
1160
1161 /* Parse audio parts */
1162 techpoint->audio_in = NULL;
1163 if (!of_property_read_string(node, "techpoint,audio-in-format", &str)) {
1164 struct techpoint_audio *audio_stream;
1165
1166 techpoint->audio_in = devm_kzalloc(dev, sizeof(struct techpoint_audio),
1167 GFP_KERNEL);
1168 if (!techpoint->audio_in)
1169 return -ENOMEM;
1170
1171 audio_stream = techpoint->audio_in;
1172
1173 if (strcmp(str, "i2s") == 0)
1174 audio_stream->audfmt = AUDFMT_I2S;
1175 else if (strcmp(str, "dsp") == 0)
1176 audio_stream->audfmt = AUDFMT_DSP;
1177 else {
1178 dev_err(dev, "techpoint,audio-in-format invalid\n");
1179 return -EINVAL;
1180 }
1181
1182 if (!of_property_read_u32(node, "techpoint,audio-in-mclk-fs", &v)) {
1183 switch (v) {
1184 case 256:
1185 break;
1186 default:
1187 dev_err(dev,
1188 "techpoint,audio-in-mclk-fs invalid\n");
1189 return -EINVAL;
1190 }
1191 audio_stream->mclk_fs = v;
1192 }
1193
1194 if (!of_property_read_u32(node, "techpoint,audio-in-cascade-num", &v))
1195 audio_stream->cascade_num = v;
1196
1197 if (!of_property_read_u32(node, "techpoint,audio-in-cascade-order", &v)) {
1198 if (v > 1)
1199 dev_err(dev,
1200 "audio-in-cascade-order should be 1st chip, otherwise without cascade (is 0)\n");
1201 else
1202 audio_stream->cascade_order = v;
1203 }
1204
1205 if (audio_stream->cascade_order == 1) {
1206 struct device_node *np;
1207 int i, count;
1208
1209 count = of_count_phandle_with_args(node,
1210 "techpoint,audio-in-cascade-slaves",
1211 NULL);
1212 if (count < 0 || count > MAX_SLAVES)
1213 return -EINVAL;
1214
1215 for (i = 0; i < count; i++) {
1216 np = of_parse_phandle(node, "techpoint,audio-in-cascade-slaves", i);
1217 if (!np)
1218 return -ENODEV;
1219 }
1220
1221 for (i = 0; i < g_idx; i++) {
1222 struct techpoint *tp = g_techpoints[i];
1223
1224 if (tp->i2c_idx != techpoint->i2c_idx) {
1225 audio_stream->slave_tp[i] = tp;
1226 audio_stream->slave_num++;
1227 }
1228 }
1229 }
1230 }
1231
1232 techpoint->audio_out = NULL;
1233 if (!of_property_read_string(node, "techpoint,audio-out-format", &str)) {
1234 struct techpoint_audio *audio_stream;
1235
1236 techpoint->audio_out = devm_kzalloc(dev, sizeof(struct techpoint_audio),
1237 GFP_KERNEL);
1238 if (!techpoint->audio_out)
1239 return -ENOMEM;
1240
1241 audio_stream = techpoint->audio_out;
1242
1243 if (strcmp(str, "i2s") == 0)
1244 audio_stream->audfmt = AUDFMT_I2S;
1245 else if (strcmp(str, "dsp") == 0)
1246 audio_stream->audfmt = AUDFMT_DSP;
1247 else {
1248 dev_err(dev, "techpoint,audio-out-format invalid\n");
1249 return -EINVAL;
1250 }
1251
1252 if (!of_property_read_u32(node, "techpoint,audio-out-mclk-fs", &v)) {
1253 switch (v) {
1254 case 256:
1255 break;
1256 default:
1257 dev_err(dev,
1258 "techpoint,audio-out-mclk-fs invalid\n");
1259 return -EINVAL;
1260 }
1261 audio_stream->mclk_fs = v;
1262 }
1263
1264 if (!of_property_read_u32(node, "techpoint,audio-out-cascade-num", &v))
1265 audio_stream->cascade_num = v;
1266
1267 if (!of_property_read_u32(node, "techpoint,audio-out-cascade-order", &v)) {
1268 if (v > 1)
1269 dev_err(dev,
1270 "audio-out-cascade-order should be 1st chip, otherwise without cascade (is 0)\n");
1271 else
1272 audio_stream->cascade_order = v;
1273 }
1274 }
1275
1276 if (!techpoint->audio_in && !techpoint->audio_out)
1277 return -ENODEV;
1278
1279 return 0;
1280 }
1281
techpoint_audio_probe(struct techpoint * techpoint)1282 static int techpoint_audio_probe(struct techpoint *techpoint)
1283 {
1284 struct device *dev = &techpoint->client->dev;
1285 int ret;
1286 unsigned char i;
1287
1288 switch (techpoint->chip_id) {
1289 case CHIP_TP9930:
1290 techpoint_9930_audio_init(techpoint);
1291 break;
1292 case CHIP_TP2855:
1293 techpoint_2855_audio_init(techpoint);
1294 break;
1295 default:
1296 break;
1297 }
1298
1299 if (techpoint->chip_id == CHIP_TP9930) {
1300
1301 techpoint_write_reg(techpoint->client, 0x40, 0x00);
1302 for (i = 0; i < 0xff; i++)
1303 techpoint_write_reg(techpoint->client, i, 0xbb);
1304 }
1305
1306 ret = techpoint_audio_dt_parse(techpoint);
1307 if (ret) {
1308 dev_info(dev, "hasn't audio DT nodes\n");
1309 return 0;
1310 }
1311
1312 ret = techpoint_audio_init(techpoint);
1313 if (ret) {
1314 dev_info(dev, "audio init failed(%d)\n", ret);
1315 return 0;
1316 }
1317
1318 ret = devm_snd_soc_register_component(dev,
1319 &techpoint_codec_driver,
1320 &techpoint_audio_dai, 1);
1321 if (ret) {
1322 dev_err(dev, "register audio codec failed\n");
1323 return -EINVAL;
1324 }
1325
1326 dev_info(dev, "registered audio codec\n");
1327
1328 return 0;
1329 }
1330
techpoint_device_release(struct device * dev)1331 static void techpoint_device_release(struct device *dev)
1332 {
1333
1334 }
1335
techpoint_sysfs_init(struct i2c_client * client,struct techpoint * techpoint)1336 static int techpoint_sysfs_init(struct i2c_client *client,
1337 struct techpoint *techpoint)
1338 {
1339 struct device *dev = &techpoint->dev;
1340 int i;
1341
1342 dev->release = techpoint_device_release;
1343 dev->parent = &client->dev;
1344 set_dev_node(dev, dev_to_node(&client->dev));
1345 dev_set_name(dev, "techpoint-dev");
1346
1347 if (device_register(dev)) {
1348 dev_err(&client->dev,
1349 "Register 'techpoint-dev' failed\n");
1350 dev->parent = NULL;
1351 return -ENOMEM;
1352 }
1353
1354 for (i = 0; i < ARRAY_SIZE(techpoint_attrs); i++) {
1355 if (device_create_file(dev, &techpoint_attrs[i])) {
1356 dev_err(&client->dev,
1357 "Create 'techpoint-dev' attr failed\n");
1358 device_unregister(dev);
1359 return -ENOMEM;
1360 }
1361 }
1362
1363 return 0;
1364 }
1365
techpoint_probe(struct i2c_client * client,const struct i2c_device_id * id)1366 static int techpoint_probe(struct i2c_client *client,
1367 const struct i2c_device_id *id)
1368 {
1369 struct device *dev = &client->dev;
1370 struct techpoint *techpoint;
1371 struct v4l2_subdev *sd;
1372 __maybe_unused char facing[2];
1373 int ret = 0, index;
1374
1375 dev_info(dev, "driver version: %02x.%02x.%02x",
1376 DRIVER_VERSION >> 16,
1377 (DRIVER_VERSION & 0xff00) >> 8, DRIVER_VERSION & 0x00ff);
1378
1379 techpoint = devm_kzalloc(dev, sizeof(*techpoint), GFP_KERNEL);
1380 if (!techpoint)
1381 return -ENOMEM;
1382
1383 techpoint->client = client;
1384 techpoint->supplies = NULL;
1385
1386 techpoint_sysfs_init(client, techpoint);
1387
1388 mutex_init(&techpoint->mutex);
1389
1390 sd = &techpoint->subdev;
1391 v4l2_i2c_subdev_init(sd, client, &techpoint_subdev_ops);
1392
1393 techpoint_analyze_dts(techpoint);
1394
1395 ret = __techpoint_power_on(techpoint);
1396 if (ret) {
1397 dev_err(dev, "Failed to power on techpoint\n");
1398 goto err_destroy_mutex;
1399 }
1400
1401 ret = techpoint_initialize_devices(techpoint);
1402 if (ret) {
1403 dev_err(dev, "Failed to initialize techpoint device\n");
1404 goto err_power_off;
1405 }
1406
1407 ret = techpoint_initialize_controls(techpoint);
1408 if (ret) {
1409 dev_err(dev, "Failed to initialize controls techpoint\n");
1410 goto err_free_handler;
1411 }
1412 #ifdef CONFIG_VIDEO_V4L2_SUBDEV_API
1413 sd->internal_ops = &techpoint_internal_ops;
1414 sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1415 #endif
1416
1417 #if defined(CONFIG_MEDIA_CONTROLLER)
1418 for (index = 0; index < PAD_MAX; index++)
1419 techpoint->pad[index].flags = MEDIA_PAD_FL_SOURCE;
1420 sd->entity.function = MEDIA_ENT_F_CAM_SENSOR;
1421 ret = media_entity_pads_init(&sd->entity, PAD_MAX, techpoint->pad);
1422 if (ret < 0)
1423 goto err_power_off;
1424 #endif
1425
1426 memset(facing, 0, sizeof(facing));
1427 if (strcmp(techpoint->module_facing, "back") == 0)
1428 facing[0] = 'b';
1429 else
1430 facing[0] = 'f';
1431
1432 snprintf(sd->name, sizeof(sd->name), "m%02d_%s_%s %s",
1433 techpoint->module_index, facing,
1434 TECHPOINT_NAME, dev_name(sd->dev));
1435
1436 ret = v4l2_async_register_subdev_sensor_common(sd);
1437 if (ret) {
1438 dev_err(dev, "v4l2 async register subdev failed\n");
1439 goto err_clean_entity;
1440 }
1441
1442 techpoint->i2c_idx = g_idx;
1443 g_techpoints[g_idx++] = techpoint;
1444
1445 ret = techpoint_audio_probe(techpoint);
1446 if (ret) {
1447 dev_err(dev, "sound audio probe failed\n");
1448 goto err_clean_entity;
1449 }
1450
1451 pm_runtime_set_active(dev);
1452 pm_runtime_enable(dev);
1453 pm_runtime_idle(dev);
1454
1455 return 0;
1456
1457 err_clean_entity:
1458 #if defined(CONFIG_MEDIA_CONTROLLER)
1459 media_entity_cleanup(&sd->entity);
1460 #endif
1461 err_free_handler:
1462 v4l2_ctrl_handler_free(&techpoint->ctrl_handler);
1463 err_power_off:
1464 __techpoint_power_off(techpoint);
1465 err_destroy_mutex:
1466 mutex_destroy(&techpoint->mutex);
1467
1468 return ret;
1469 }
1470
techpoint_remove(struct i2c_client * client)1471 static int techpoint_remove(struct i2c_client *client)
1472 {
1473 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1474 struct techpoint *techpoint = to_techpoint(sd);
1475
1476 v4l2_async_unregister_subdev(sd);
1477 #if defined(CONFIG_MEDIA_CONTROLLER)
1478 media_entity_cleanup(&sd->entity);
1479 #endif
1480 v4l2_ctrl_handler_free(&techpoint->ctrl_handler);
1481 mutex_destroy(&techpoint->mutex);
1482
1483 pm_runtime_disable(&client->dev);
1484 if (!pm_runtime_status_suspended(&client->dev))
1485 __techpoint_power_off(techpoint);
1486 pm_runtime_set_suspended(&client->dev);
1487
1488 return 0;
1489 }
1490
1491 static struct i2c_driver techpoint_i2c_driver = {
1492 .driver = {
1493 .name = TECHPOINT_NAME,
1494 .pm = &techpoint_pm_ops,
1495 .of_match_table = of_match_ptr(techpoint_of_match),
1496 },
1497 .probe = &techpoint_probe,
1498 .remove = &techpoint_remove,
1499 .id_table = techpoint_match_id,
1500 };
1501
sensor_mod_init(void)1502 static int __init sensor_mod_init(void)
1503 {
1504 return i2c_add_driver(&techpoint_i2c_driver);
1505 }
1506
sensor_mod_exit(void)1507 static void __exit sensor_mod_exit(void)
1508 {
1509 i2c_del_driver(&techpoint_i2c_driver);
1510 }
1511
1512 device_initcall_sync(sensor_mod_init);
1513 module_exit(sensor_mod_exit);
1514
1515 MODULE_AUTHOR("Vicent Chi <vicent.chi@rock-chips.com>");
1516 MODULE_DESCRIPTION("Techpoint decoder driver");
1517 MODULE_LICENSE("GPL");
1518