Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / i2c / sil164_drv.c
blob741886b5441973b19816a229a4a317dd0c14bf2a
1 /*
2 * Copyright (C) 2010 Francisco Jerez.
3 * All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <linux/module.h>
29 #include <drm/drm_drv.h>
30 #include <drm/drm_encoder_slave.h>
31 #include <drm/drm_print.h>
32 #include <drm/drm_probe_helper.h>
33 #include <drm/i2c/sil164.h>
35 struct sil164_priv {
36 struct sil164_encoder_params config;
37 struct i2c_client *duallink_slave;
39 uint8_t saved_state[0x10];
40 uint8_t saved_slave_state[0x10];
43 #define to_sil164_priv(x) \
44 ((struct sil164_priv *)to_encoder_slave(x)->slave_priv)
46 #define sil164_dbg(client, format, ...) do { \
47 if (drm_debug_enabled(DRM_UT_KMS)) \
48 dev_printk(KERN_DEBUG, &client->dev, \
49 "%s: " format, __func__, ## __VA_ARGS__); \
50 } while (0)
51 #define sil164_info(client, format, ...) \
52 dev_info(&client->dev, format, __VA_ARGS__)
53 #define sil164_err(client, format, ...) \
54 dev_err(&client->dev, format, __VA_ARGS__)
56 #define SIL164_I2C_ADDR_MASTER 0x38
57 #define SIL164_I2C_ADDR_SLAVE 0x39
59 /* HW register definitions */
61 #define SIL164_VENDOR_LO 0x0
62 #define SIL164_VENDOR_HI 0x1
63 #define SIL164_DEVICE_LO 0x2
64 #define SIL164_DEVICE_HI 0x3
65 #define SIL164_REVISION 0x4
66 #define SIL164_FREQ_MIN 0x6
67 #define SIL164_FREQ_MAX 0x7
68 #define SIL164_CONTROL0 0x8
69 # define SIL164_CONTROL0_POWER_ON 0x01
70 # define SIL164_CONTROL0_EDGE_RISING 0x02
71 # define SIL164_CONTROL0_INPUT_24BIT 0x04
72 # define SIL164_CONTROL0_DUAL_EDGE 0x08
73 # define SIL164_CONTROL0_HSYNC_ON 0x10
74 # define SIL164_CONTROL0_VSYNC_ON 0x20
75 #define SIL164_DETECT 0x9
76 # define SIL164_DETECT_INTR_STAT 0x01
77 # define SIL164_DETECT_HOTPLUG_STAT 0x02
78 # define SIL164_DETECT_RECEIVER_STAT 0x04
79 # define SIL164_DETECT_INTR_MODE_RECEIVER 0x00
80 # define SIL164_DETECT_INTR_MODE_HOTPLUG 0x08
81 # define SIL164_DETECT_OUT_MODE_HIGH 0x00
82 # define SIL164_DETECT_OUT_MODE_INTR 0x10
83 # define SIL164_DETECT_OUT_MODE_RECEIVER 0x20
84 # define SIL164_DETECT_OUT_MODE_HOTPLUG 0x30
85 # define SIL164_DETECT_VSWING_STAT 0x80
86 #define SIL164_CONTROL1 0xa
87 # define SIL164_CONTROL1_DESKEW_ENABLE 0x10
88 # define SIL164_CONTROL1_DESKEW_INCR_SHIFT 5
89 #define SIL164_GPIO 0xb
90 #define SIL164_CONTROL2 0xc
91 # define SIL164_CONTROL2_FILTER_ENABLE 0x01
92 # define SIL164_CONTROL2_FILTER_SETTING_SHIFT 1
93 # define SIL164_CONTROL2_DUALLINK_MASTER 0x40
94 # define SIL164_CONTROL2_SYNC_CONT 0x80
95 #define SIL164_DUALLINK 0xd
96 # define SIL164_DUALLINK_ENABLE 0x10
97 # define SIL164_DUALLINK_SKEW_SHIFT 5
98 #define SIL164_PLLZONE 0xe
99 # define SIL164_PLLZONE_STAT 0x08
100 # define SIL164_PLLZONE_FORCE_ON 0x10
101 # define SIL164_PLLZONE_FORCE_HIGH 0x20
103 /* HW access functions */
105 static void
106 sil164_write(struct i2c_client *client, uint8_t addr, uint8_t val)
108 uint8_t buf[] = {addr, val};
109 int ret;
111 ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
112 if (ret < 0)
113 sil164_err(client, "Error %d writing to subaddress 0x%x\n",
114 ret, addr);
117 static uint8_t
118 sil164_read(struct i2c_client *client, uint8_t addr)
120 uint8_t val;
121 int ret;
123 ret = i2c_master_send(client, &addr, sizeof(addr));
124 if (ret < 0)
125 goto fail;
127 ret = i2c_master_recv(client, &val, sizeof(val));
128 if (ret < 0)
129 goto fail;
131 return val;
133 fail:
134 sil164_err(client, "Error %d reading from subaddress 0x%x\n",
135 ret, addr);
136 return 0;
139 static void
140 sil164_save_state(struct i2c_client *client, uint8_t *state)
142 int i;
144 for (i = 0x8; i <= 0xe; i++)
145 state[i] = sil164_read(client, i);
148 static void
149 sil164_restore_state(struct i2c_client *client, uint8_t *state)
151 int i;
153 for (i = 0x8; i <= 0xe; i++)
154 sil164_write(client, i, state[i]);
157 static void
158 sil164_set_power_state(struct i2c_client *client, bool on)
160 uint8_t control0 = sil164_read(client, SIL164_CONTROL0);
162 if (on)
163 control0 |= SIL164_CONTROL0_POWER_ON;
164 else
165 control0 &= ~SIL164_CONTROL0_POWER_ON;
167 sil164_write(client, SIL164_CONTROL0, control0);
170 static void
171 sil164_init_state(struct i2c_client *client,
172 struct sil164_encoder_params *config,
173 bool duallink)
175 sil164_write(client, SIL164_CONTROL0,
176 SIL164_CONTROL0_HSYNC_ON |
177 SIL164_CONTROL0_VSYNC_ON |
178 (config->input_edge ? SIL164_CONTROL0_EDGE_RISING : 0) |
179 (config->input_width ? SIL164_CONTROL0_INPUT_24BIT : 0) |
180 (config->input_dual ? SIL164_CONTROL0_DUAL_EDGE : 0));
182 sil164_write(client, SIL164_DETECT,
183 SIL164_DETECT_INTR_STAT |
184 SIL164_DETECT_OUT_MODE_RECEIVER);
186 sil164_write(client, SIL164_CONTROL1,
187 (config->input_skew ? SIL164_CONTROL1_DESKEW_ENABLE : 0) |
188 (((config->input_skew + 4) & 0x7)
189 << SIL164_CONTROL1_DESKEW_INCR_SHIFT));
191 sil164_write(client, SIL164_CONTROL2,
192 SIL164_CONTROL2_SYNC_CONT |
193 (config->pll_filter ? 0 : SIL164_CONTROL2_FILTER_ENABLE) |
194 (4 << SIL164_CONTROL2_FILTER_SETTING_SHIFT));
196 sil164_write(client, SIL164_PLLZONE, 0);
198 if (duallink)
199 sil164_write(client, SIL164_DUALLINK,
200 SIL164_DUALLINK_ENABLE |
201 (((config->duallink_skew + 4) & 0x7)
202 << SIL164_DUALLINK_SKEW_SHIFT));
203 else
204 sil164_write(client, SIL164_DUALLINK, 0);
207 /* DRM encoder functions */
209 static void
210 sil164_encoder_set_config(struct drm_encoder *encoder, void *params)
212 struct sil164_priv *priv = to_sil164_priv(encoder);
214 priv->config = *(struct sil164_encoder_params *)params;
217 static void
218 sil164_encoder_dpms(struct drm_encoder *encoder, int mode)
220 struct sil164_priv *priv = to_sil164_priv(encoder);
221 bool on = (mode == DRM_MODE_DPMS_ON);
222 bool duallink = (on && encoder->crtc->mode.clock > 165000);
224 sil164_set_power_state(drm_i2c_encoder_get_client(encoder), on);
226 if (priv->duallink_slave)
227 sil164_set_power_state(priv->duallink_slave, duallink);
230 static void
231 sil164_encoder_save(struct drm_encoder *encoder)
233 struct sil164_priv *priv = to_sil164_priv(encoder);
235 sil164_save_state(drm_i2c_encoder_get_client(encoder),
236 priv->saved_state);
238 if (priv->duallink_slave)
239 sil164_save_state(priv->duallink_slave,
240 priv->saved_slave_state);
243 static void
244 sil164_encoder_restore(struct drm_encoder *encoder)
246 struct sil164_priv *priv = to_sil164_priv(encoder);
248 sil164_restore_state(drm_i2c_encoder_get_client(encoder),
249 priv->saved_state);
251 if (priv->duallink_slave)
252 sil164_restore_state(priv->duallink_slave,
253 priv->saved_slave_state);
256 static int
257 sil164_encoder_mode_valid(struct drm_encoder *encoder,
258 struct drm_display_mode *mode)
260 struct sil164_priv *priv = to_sil164_priv(encoder);
262 if (mode->clock < 32000)
263 return MODE_CLOCK_LOW;
265 if (mode->clock > 330000 ||
266 (mode->clock > 165000 && !priv->duallink_slave))
267 return MODE_CLOCK_HIGH;
269 return MODE_OK;
272 static void
273 sil164_encoder_mode_set(struct drm_encoder *encoder,
274 struct drm_display_mode *mode,
275 struct drm_display_mode *adjusted_mode)
277 struct sil164_priv *priv = to_sil164_priv(encoder);
278 bool duallink = adjusted_mode->clock > 165000;
280 sil164_init_state(drm_i2c_encoder_get_client(encoder),
281 &priv->config, duallink);
283 if (priv->duallink_slave)
284 sil164_init_state(priv->duallink_slave,
285 &priv->config, duallink);
287 sil164_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
290 static enum drm_connector_status
291 sil164_encoder_detect(struct drm_encoder *encoder,
292 struct drm_connector *connector)
294 struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
296 if (sil164_read(client, SIL164_DETECT) & SIL164_DETECT_HOTPLUG_STAT)
297 return connector_status_connected;
298 else
299 return connector_status_disconnected;
302 static int
303 sil164_encoder_get_modes(struct drm_encoder *encoder,
304 struct drm_connector *connector)
306 return 0;
309 static int
310 sil164_encoder_create_resources(struct drm_encoder *encoder,
311 struct drm_connector *connector)
313 return 0;
316 static int
317 sil164_encoder_set_property(struct drm_encoder *encoder,
318 struct drm_connector *connector,
319 struct drm_property *property,
320 uint64_t val)
322 return 0;
325 static void
326 sil164_encoder_destroy(struct drm_encoder *encoder)
328 struct sil164_priv *priv = to_sil164_priv(encoder);
330 i2c_unregister_device(priv->duallink_slave);
332 kfree(priv);
333 drm_i2c_encoder_destroy(encoder);
336 static const struct drm_encoder_slave_funcs sil164_encoder_funcs = {
337 .set_config = sil164_encoder_set_config,
338 .destroy = sil164_encoder_destroy,
339 .dpms = sil164_encoder_dpms,
340 .save = sil164_encoder_save,
341 .restore = sil164_encoder_restore,
342 .mode_valid = sil164_encoder_mode_valid,
343 .mode_set = sil164_encoder_mode_set,
344 .detect = sil164_encoder_detect,
345 .get_modes = sil164_encoder_get_modes,
346 .create_resources = sil164_encoder_create_resources,
347 .set_property = sil164_encoder_set_property,
350 /* I2C driver functions */
352 static int
353 sil164_probe(struct i2c_client *client, const struct i2c_device_id *id)
355 int vendor = sil164_read(client, SIL164_VENDOR_HI) << 8 |
356 sil164_read(client, SIL164_VENDOR_LO);
357 int device = sil164_read(client, SIL164_DEVICE_HI) << 8 |
358 sil164_read(client, SIL164_DEVICE_LO);
359 int rev = sil164_read(client, SIL164_REVISION);
361 if (vendor != 0x1 || device != 0x6) {
362 sil164_dbg(client, "Unknown device %x:%x.%x\n",
363 vendor, device, rev);
364 return -ENODEV;
367 sil164_info(client, "Detected device %x:%x.%x\n",
368 vendor, device, rev);
370 return 0;
373 static int
374 sil164_remove(struct i2c_client *client)
376 return 0;
379 static struct i2c_client *
380 sil164_detect_slave(struct i2c_client *client)
382 struct i2c_adapter *adap = client->adapter;
383 struct i2c_msg msg = {
384 .addr = SIL164_I2C_ADDR_SLAVE,
385 .len = 0,
387 const struct i2c_board_info info = {
388 I2C_BOARD_INFO("sil164", SIL164_I2C_ADDR_SLAVE)
391 if (i2c_transfer(adap, &msg, 1) != 1) {
392 sil164_dbg(adap, "No dual-link slave found.");
393 return NULL;
396 return i2c_new_client_device(adap, &info);
399 static int
400 sil164_encoder_init(struct i2c_client *client,
401 struct drm_device *dev,
402 struct drm_encoder_slave *encoder)
404 struct sil164_priv *priv;
405 struct i2c_client *slave_client;
407 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
408 if (!priv)
409 return -ENOMEM;
411 encoder->slave_priv = priv;
412 encoder->slave_funcs = &sil164_encoder_funcs;
414 slave_client = sil164_detect_slave(client);
415 if (!IS_ERR(slave_client))
416 priv->duallink_slave = slave_client;
418 return 0;
421 static const struct i2c_device_id sil164_ids[] = {
422 { "sil164", 0 },
425 MODULE_DEVICE_TABLE(i2c, sil164_ids);
427 static struct drm_i2c_encoder_driver sil164_driver = {
428 .i2c_driver = {
429 .probe = sil164_probe,
430 .remove = sil164_remove,
431 .driver = {
432 .name = "sil164",
434 .id_table = sil164_ids,
436 .encoder_init = sil164_encoder_init,
439 /* Module initialization */
441 static int __init
442 sil164_init(void)
444 return drm_i2c_encoder_register(THIS_MODULE, &sil164_driver);
447 static void __exit
448 sil164_exit(void)
450 drm_i2c_encoder_unregister(&sil164_driver);
453 MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
454 MODULE_DESCRIPTION("Silicon Image sil164 TMDS transmitter driver");
455 MODULE_LICENSE("GPL and additional rights");
457 module_init(sil164_init);
458 module_exit(sil164_exit);