• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Driver for rockchip rk1000 tv encoder
3  *  Copyright (C) 2017 Fuzhou Rockchip Electronics Co.Ltd
4  *  Author:
5  *      Algea Cao <algea.cao@rock-chips.com>
6  *  This program is free software; you can redistribute  it and/or modify it
7  *  under  the terms of  the GNU General  Public License as published by the
8  *  Free Software Foundation;  either version 2 of the  License, or (at your
9  *  option) any later version.
10  */
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/regmap.h>
16 #include <linux/mfd/syscon.h>
17 
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_probe_helper.h>
22 
23 #include "../rockchip/rockchip_drm_drv.h"
24 
25 #define TVE_POWCR 0x03
26 #define TVE_OFF 0X07
27 #define TVE_ON 0x03
28 
29 static const struct drm_display_mode rk1000_cvbs_mode[2] = {
30 	{ DRM_MODE("720x576", DRM_MODE_TYPE_DRIVER, 27000, 720, 732,
31 		   738, 864, 0, 576, 582, 588, 625, 0,
32 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
33 		   0, },
34 	{ DRM_MODE("720x480", DRM_MODE_TYPE_DRIVER, 27000, 720, 736,
35 		   742, 858, 0, 480, 486, 492, 529, 0,
36 		   DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
37 		   0, },
38 };
39 
40 struct rk1000_tve {
41 	struct device *dev;
42 	struct i2c_client *client;
43 	struct drm_connector connector;
44 	struct drm_bridge bridge;
45 	struct drm_encoder *encoder;
46 	int mode;
47 	struct regmap *ctlmap;
48 	struct regmap *tvemap;
49 	u32 preferred_mode;
50 	struct rockchip_drm_sub_dev sub_dev;
51 };
52 
53 enum {
54 	CVBS_NTSC = 0,
55 	CVBS_PAL,
56 };
57 
58 static const u8 pal_tve_regs[] = {0x06, 0x00, 0x00, 0x03, 0x00, 0x00};
59 static const u8 pal_ctl_regs[] = {0x41, 0x01};
60 
61 static const u8 ntsc_tve_regs[] = {0x00, 0x00, 0x00, 0x03, 0x00, 0x00};
62 static const u8 ntsc_ctl_regs[] = {0x43, 0x01};
63 
64 static const struct regmap_range rk1000_tve_volatile_ranges[] = {
65 	{ .range_min = 0x00, .range_max = 0x05 },
66 };
67 
68 static const struct regmap_access_table rk1000_tve_reg_table = {
69 	.yes_ranges = rk1000_tve_volatile_ranges,
70 	.n_yes_ranges = ARRAY_SIZE(rk1000_tve_volatile_ranges),
71 };
72 
73 static const struct regmap_config rk1000_tve_regmap_config = {
74 	.reg_bits = 8,
75 	.val_bits = 8,
76 	.cache_type = REGCACHE_RBTREE,
77 	.volatile_table = &rk1000_tve_reg_table,
78 };
79 
80 static const struct regmap_range rk1000_ctl_volatile_ranges[] = {
81 	{ .range_min = 0x03, .range_max = 0x04 },
82 };
83 
84 static const struct regmap_access_table rk1000_ctl_reg_table = {
85 	.yes_ranges = rk1000_ctl_volatile_ranges,
86 	.n_yes_ranges = ARRAY_SIZE(rk1000_ctl_volatile_ranges),
87 };
88 
89 static const struct regmap_config rk1000_ctl_regmap_config = {
90 	.reg_bits = 8,
91 	.val_bits = 8,
92 	.cache_type = REGCACHE_RBTREE,
93 	.volatile_table = &rk1000_ctl_reg_table,
94 };
95 
bridge_to_rk1000(struct drm_bridge * bridge)96 static struct rk1000_tve *bridge_to_rk1000(struct drm_bridge *bridge)
97 {
98 	return container_of(bridge, struct rk1000_tve, bridge);
99 }
100 
connector_to_rk1000(struct drm_connector * connector)101 static struct rk1000_tve *connector_to_rk1000(struct drm_connector *connector)
102 {
103 	return container_of(connector, struct rk1000_tve, connector);
104 }
105 
rk1000_tv_write_block(struct rk1000_tve * rk1000,u8 reg,const u8 * buf,u8 len)106 static int rk1000_tv_write_block(struct rk1000_tve *rk1000,
107 				 u8 reg, const u8 *buf, u8 len)
108 {
109 	int i, ret = 0;
110 
111 	for (i = 0; i < len; i++) {
112 		ret = regmap_write(rk1000->tvemap, reg + i, buf[i]);
113 		if (ret)
114 			break;
115 	}
116 
117 	return ret;
118 }
119 
rk1000_control_write_block(struct rk1000_tve * rk1000,u8 reg,const u8 * buf,u8 len)120 static int rk1000_control_write_block(struct rk1000_tve *rk1000,
121 				      u8 reg, const u8 *buf, u8 len)
122 {
123 	int i, ret = 0;
124 
125 	for (i = 0; i < len; i++) {
126 		ret = regmap_write(rk1000->ctlmap, reg + i, buf[i]);
127 		if (ret)
128 			break;
129 	}
130 
131 	return ret;
132 }
133 
rk1000_mode_set(struct rk1000_tve * rk1000)134 static int rk1000_mode_set(struct rk1000_tve *rk1000)
135 {
136 	int ret;
137 	const u8 *tv_regs, *control_regs;
138 
139 	switch (rk1000->mode) {
140 	case CVBS_PAL:
141 		dev_dbg(rk1000->dev, "rk1000 PAL\n");
142 		tv_regs = pal_tve_regs;
143 		control_regs = pal_ctl_regs;
144 		break;
145 	case CVBS_NTSC:
146 		dev_dbg(rk1000->dev, "rk1000 NTSC\n");
147 		tv_regs = ntsc_tve_regs;
148 		control_regs = ntsc_ctl_regs;
149 		break;
150 	default:
151 		dev_dbg(rk1000->dev, "mode select err\n");
152 		return -EINVAL;
153 	}
154 
155 	ret = rk1000_tv_write_block(rk1000, 0, tv_regs, 6);
156 	if (ret) {
157 		dev_err(rk1000->dev, "rk1000_tv_write_block err!\n");
158 		return ret;
159 	}
160 
161 	ret = rk1000_control_write_block(rk1000, 0x03, control_regs, 2);
162 	if (ret < 0) {
163 		dev_err(rk1000->dev, "rk1000 control write block err\n");
164 		return ret;
165 	}
166 
167 	return ret;
168 }
169 
rk1000_tve_disable(struct rk1000_tve * rk1000)170 static int rk1000_tve_disable(struct rk1000_tve *rk1000)
171 {
172 	int ret;
173 	u8 val;
174 
175 	val = TVE_OFF;
176 	ret = rk1000_tv_write_block(rk1000, TVE_POWCR, &val, 1);
177 
178 	return ret;
179 }
180 
rk1000_tve_enable(struct rk1000_tve * rk1000)181 static int rk1000_tve_enable(struct rk1000_tve *rk1000)
182 {
183 	int ret;
184 	u8 val;
185 
186 	dev_dbg(rk1000->dev, "%s\n", __func__);
187 	ret = rk1000_mode_set(rk1000);
188 	if (ret)
189 		return ret;
190 
191 	val = TVE_ON;
192 	ret = rk1000_tv_write_block(rk1000, TVE_POWCR, &val, 1);
193 	if (ret)
194 		return ret;
195 
196 	return 0;
197 }
198 
199 static enum drm_mode_status
rk1000_mode_valid(struct drm_connector * connector,struct drm_display_mode * mode)200 rk1000_mode_valid(struct drm_connector *connector,
201 		  struct drm_display_mode *mode)
202 {
203 	return MODE_OK;
204 }
205 
206 static int
rk1000_get_modes(struct drm_connector * connector)207 rk1000_get_modes(struct drm_connector *connector)
208 {
209 	int count;
210 	struct rk1000_tve *rk1000 = connector_to_rk1000(connector);
211 
212 	for (count = 0; count < ARRAY_SIZE(rk1000_cvbs_mode); count++) {
213 		struct drm_display_mode *mode_ptr;
214 
215 		mode_ptr = drm_mode_duplicate(connector->dev,
216 					      &rk1000_cvbs_mode[count]);
217 		if (!mode_ptr) {
218 			dev_err(rk1000->dev, "mode duplicate failed\n");
219 			return -ENOMEM;
220 		}
221 		if (rk1000->preferred_mode == count)
222 			mode_ptr->type |= DRM_MODE_TYPE_PREFERRED;
223 		drm_mode_probed_add(connector, mode_ptr);
224 	}
225 
226 	return count;
227 }
228 
229 static enum drm_connector_status
rk1000_connector_detect(struct drm_connector * connector,bool force)230 rk1000_connector_detect(struct drm_connector *connector,
231 			bool force)
232 {
233 	return connector_status_connected;
234 }
235 
rk1000_best_encoder(struct drm_connector * connector)236 static struct drm_encoder *rk1000_best_encoder(struct drm_connector *connector)
237 {
238 	struct rk1000_tve *rk1000 = connector_to_rk1000(connector);
239 
240 	return rk1000->encoder;
241 }
242 
243 static
244 const struct drm_connector_helper_funcs rk1000_connector_helper_funcs = {
245 	.get_modes = rk1000_get_modes,
246 	.mode_valid = rk1000_mode_valid,
247 	.best_encoder = rk1000_best_encoder,
248 };
249 
250 static const struct drm_connector_funcs rk1000_connector_funcs = {
251 	.fill_modes = drm_helper_probe_single_connector_modes,
252 	.detect = rk1000_connector_detect,
253 	.destroy = drm_connector_cleanup,
254 	.reset = drm_atomic_helper_connector_reset,
255 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
256 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
257 };
258 
259 static void
rk1000_bridge_mode_set(struct drm_bridge * bridge,const struct drm_display_mode * mode,const struct drm_display_mode * adjusted_mode)260 rk1000_bridge_mode_set(struct drm_bridge *bridge,
261 		       const struct drm_display_mode *mode,
262 		       const struct drm_display_mode *adjusted_mode)
263 {
264 	struct rk1000_tve *rk1000;
265 
266 	rk1000 = bridge_to_rk1000(bridge);
267 	dev_dbg(rk1000->dev, "encoder mode set:%s\n", adjusted_mode->name);
268 
269 	if (adjusted_mode->vdisplay == 576)
270 		rk1000->mode = CVBS_PAL;
271 	else
272 		rk1000->mode = CVBS_NTSC;
273 }
274 
rk1000_bridge_enable(struct drm_bridge * bridge)275 static void rk1000_bridge_enable(struct drm_bridge *bridge)
276 {
277 	int ret;
278 	struct rk1000_tve *rk1000 = bridge_to_rk1000(bridge);
279 
280 	dev_dbg(rk1000->dev, "%s\n",  __func__);
281 	ret = rk1000_tve_enable(rk1000);
282 	if (ret)
283 		dev_err(rk1000->dev, "rk1000 enable failed\n");
284 }
285 
rk1000_bridge_disable(struct drm_bridge * bridge)286 static void rk1000_bridge_disable(struct drm_bridge *bridge)
287 {
288 	struct rk1000_tve *rk1000 = bridge_to_rk1000(bridge);
289 
290 	dev_dbg(rk1000->dev, "%s\n",  __func__);
291 	rk1000_tve_disable(rk1000);
292 }
293 
rk1000_bridge_attach(struct drm_bridge * bridge,enum drm_bridge_attach_flags flags)294 static int rk1000_bridge_attach(struct drm_bridge *bridge,
295 				enum drm_bridge_attach_flags flags)
296 {
297 	struct rk1000_tve *rk1000 = bridge_to_rk1000(bridge);
298 	int ret;
299 
300 	if (!bridge->encoder) {
301 		dev_err(rk1000->dev, "Parent encoder object not found\n");
302 		return -ENODEV;
303 	}
304 
305 	rk1000->encoder = bridge->encoder;
306 	drm_connector_helper_add(&rk1000->connector,
307 				 &rk1000_connector_helper_funcs);
308 
309 	ret = drm_connector_init(bridge->dev, &rk1000->connector,
310 				 &rk1000_connector_funcs,
311 				 DRM_MODE_CONNECTOR_TV);
312 	if (ret) {
313 		dev_err(rk1000->dev, "Failed to initialize connector\n");
314 		return ret;
315 	}
316 	ret = drm_connector_attach_encoder(&rk1000->connector,
317 					   bridge->encoder);
318 	if (ret)
319 		dev_err(rk1000->dev, "rk1000 attach failed ret:%d", ret);
320 
321 	rk1000->sub_dev.connector = &rk1000->connector;
322 	rk1000->sub_dev.of_node = rk1000->dev->of_node;
323 	rockchip_drm_register_sub_dev(&rk1000->sub_dev);
324 
325 	return ret;
326 }
327 
rk1000_bridge_detach(struct drm_bridge * bridge)328 static void rk1000_bridge_detach(struct drm_bridge *bridge)
329 {
330 	struct rk1000_tve *rk1000 = bridge_to_rk1000(bridge);
331 
332 	rockchip_drm_unregister_sub_dev(&rk1000->sub_dev);
333 }
334 
335 static struct drm_bridge_funcs rk1000_bridge_funcs = {
336 	.enable = rk1000_bridge_enable,
337 	.disable = rk1000_bridge_disable,
338 	.mode_set = rk1000_bridge_mode_set,
339 	.attach = rk1000_bridge_attach,
340 	.detach = rk1000_bridge_detach,
341 };
342 
rk1000_probe(struct i2c_client * client,const struct i2c_device_id * id)343 static int rk1000_probe(struct i2c_client *client,
344 			const struct i2c_device_id *id)
345 {
346 	int ret;
347 	struct device_node *np;
348 	struct device_node *ctl;
349 	struct rk1000_tve *rk1000;
350 	struct i2c_client *ctl_client;
351 
352 	rk1000 = devm_kzalloc(&client->dev, sizeof(*rk1000), GFP_KERNEL);
353 	if (!rk1000)
354 		return -ENOMEM;
355 
356 	np = client->dev.of_node;
357 	rk1000->client = client;
358 	rk1000->dev = &client->dev;
359 	rk1000->mode = CVBS_PAL;
360 
361 	rk1000->tvemap = devm_regmap_init_i2c(client,
362 					      &rk1000_tve_regmap_config);
363 	if (IS_ERR(rk1000->tvemap)) {
364 		ret = PTR_ERR(rk1000->tvemap);
365 		dev_err(rk1000->dev, "Failed to initialize tve regmap: %d\n",
366 			ret);
367 		return ret;
368 	}
369 
370 	ctl = of_parse_phandle(np, "rockchip,ctl", 0);
371 	if (!ctl) {
372 		dev_err(rk1000->dev, "rk1000 can't find control node\n");
373 		return -EINVAL;
374 	}
375 
376 	ctl_client = of_find_i2c_device_by_node(ctl);
377 	if (!ctl_client) {
378 		dev_err(rk1000->dev, "rk1000 can't find control client\n");
379 		return -EPROBE_DEFER;
380 	}
381 
382 	rk1000->ctlmap = devm_regmap_init_i2c(ctl_client,
383 					      &rk1000_ctl_regmap_config);
384 	if (IS_ERR(rk1000->ctlmap)) {
385 		ret = PTR_ERR(rk1000->ctlmap);
386 		dev_err(rk1000->dev, "Failed to initialize ctl regmap: %d\n",
387 			ret);
388 		return ret;
389 	}
390 
391 	ret = of_property_read_u32(np, "rockchip,tvemode",
392 				   &rk1000->preferred_mode);
393 	if (ret < 0) {
394 		rk1000->preferred_mode = 0;
395 	} else if (rk1000->preferred_mode > 1) {
396 		dev_err(rk1000->dev, "rk1000 tve mode value invalid\n");
397 		return -EINVAL;
398 	}
399 
400 	rk1000->bridge.funcs = &rk1000_bridge_funcs;
401 	rk1000->bridge.of_node = rk1000->dev->of_node;
402 
403 	drm_bridge_add(&rk1000->bridge);
404 
405 	i2c_set_clientdata(client, rk1000);
406 	dev_dbg(rk1000->dev, "rk1000 probe ok\n");
407 
408 	return 0;
409 }
410 
rk1000_remove(struct i2c_client * client)411 static int rk1000_remove(struct i2c_client *client)
412 {
413 	struct rk1000_tve *rk1000 = i2c_get_clientdata(client);
414 
415 	drm_bridge_remove(&rk1000->bridge);
416 
417 	return 0;
418 }
419 
420 static const struct i2c_device_id rk1000_i2c_id[] = {
421 	{ "rk1000-tve", 0 },
422 	{ }
423 };
424 
425 MODULE_DEVICE_TABLE(i2c, rk1000_i2c_id);
426 
427 static const struct of_device_id rk1000_dt_ids[] = {
428 	{ .compatible = "rockchip,rk1000-tve" },
429 	{ }
430 };
431 
432 MODULE_DEVICE_TABLE(of, rk1000_dt_ids);
433 
434 static struct i2c_driver rk1000_driver = {
435 	.driver = {
436 		.name = "rk1000-tve",
437 		.of_match_table = of_match_ptr(rk1000_dt_ids),
438 	},
439 	.id_table = rk1000_i2c_id,
440 	.probe = rk1000_probe,
441 	.remove = rk1000_remove,
442 };
443 module_i2c_driver(rk1000_driver);
444 
445 MODULE_AUTHOR("Algea Cao <Algea.cao@rock-chips.com>");
446 MODULE_DESCRIPTION("ROCKCHIP RK1000 Driver");
447 MODULE_LICENSE("GPL v2");
448