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