sil164_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * Copyright (C) 2010 Francisco Jerez.
  3. * All Rights Reserved.
  4. *
  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:
  12. *
  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.
  16. *
  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.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <drm/drm_drv.h>
  28. #include <drm/drm_encoder_slave.h>
  29. #include <drm/drm_print.h>
  30. #include <drm/drm_probe_helper.h>
  31. #include <drm/i2c/sil164.h>
  32. struct sil164_priv {
  33. struct sil164_encoder_params config;
  34. struct i2c_client *duallink_slave;
  35. uint8_t saved_state[0x10];
  36. uint8_t saved_slave_state[0x10];
  37. };
  38. #define to_sil164_priv(x) \
  39. ((struct sil164_priv *)to_encoder_slave(x)->slave_priv)
  40. #define sil164_dbg(client, format, ...) do { \
  41. if (drm_debug_enabled(DRM_UT_KMS)) \
  42. dev_printk(KERN_DEBUG, &client->dev, \
  43. "%s: " format, __func__, ## __VA_ARGS__); \
  44. } while (0)
  45. #define sil164_info(client, format, ...) \
  46. dev_info(&client->dev, format, __VA_ARGS__)
  47. #define sil164_err(client, format, ...) \
  48. dev_err(&client->dev, format, __VA_ARGS__)
  49. #define SIL164_I2C_ADDR_MASTER 0x38
  50. #define SIL164_I2C_ADDR_SLAVE 0x39
  51. /* HW register definitions */
  52. #define SIL164_VENDOR_LO 0x0
  53. #define SIL164_VENDOR_HI 0x1
  54. #define SIL164_DEVICE_LO 0x2
  55. #define SIL164_DEVICE_HI 0x3
  56. #define SIL164_REVISION 0x4
  57. #define SIL164_FREQ_MIN 0x6
  58. #define SIL164_FREQ_MAX 0x7
  59. #define SIL164_CONTROL0 0x8
  60. # define SIL164_CONTROL0_POWER_ON 0x01
  61. # define SIL164_CONTROL0_EDGE_RISING 0x02
  62. # define SIL164_CONTROL0_INPUT_24BIT 0x04
  63. # define SIL164_CONTROL0_DUAL_EDGE 0x08
  64. # define SIL164_CONTROL0_HSYNC_ON 0x10
  65. # define SIL164_CONTROL0_VSYNC_ON 0x20
  66. #define SIL164_DETECT 0x9
  67. # define SIL164_DETECT_INTR_STAT 0x01
  68. # define SIL164_DETECT_HOTPLUG_STAT 0x02
  69. # define SIL164_DETECT_RECEIVER_STAT 0x04
  70. # define SIL164_DETECT_INTR_MODE_RECEIVER 0x00
  71. # define SIL164_DETECT_INTR_MODE_HOTPLUG 0x08
  72. # define SIL164_DETECT_OUT_MODE_HIGH 0x00
  73. # define SIL164_DETECT_OUT_MODE_INTR 0x10
  74. # define SIL164_DETECT_OUT_MODE_RECEIVER 0x20
  75. # define SIL164_DETECT_OUT_MODE_HOTPLUG 0x30
  76. # define SIL164_DETECT_VSWING_STAT 0x80
  77. #define SIL164_CONTROL1 0xa
  78. # define SIL164_CONTROL1_DESKEW_ENABLE 0x10
  79. # define SIL164_CONTROL1_DESKEW_INCR_SHIFT 5
  80. #define SIL164_GPIO 0xb
  81. #define SIL164_CONTROL2 0xc
  82. # define SIL164_CONTROL2_FILTER_ENABLE 0x01
  83. # define SIL164_CONTROL2_FILTER_SETTING_SHIFT 1
  84. # define SIL164_CONTROL2_DUALLINK_MASTER 0x40
  85. # define SIL164_CONTROL2_SYNC_CONT 0x80
  86. #define SIL164_DUALLINK 0xd
  87. # define SIL164_DUALLINK_ENABLE 0x10
  88. # define SIL164_DUALLINK_SKEW_SHIFT 5
  89. #define SIL164_PLLZONE 0xe
  90. # define SIL164_PLLZONE_STAT 0x08
  91. # define SIL164_PLLZONE_FORCE_ON 0x10
  92. # define SIL164_PLLZONE_FORCE_HIGH 0x20
  93. /* HW access functions */
  94. static void
  95. sil164_write(struct i2c_client *client, uint8_t addr, uint8_t val)
  96. {
  97. uint8_t buf[] = {addr, val};
  98. int ret;
  99. ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
  100. if (ret < 0)
  101. sil164_err(client, "Error %d writing to subaddress 0x%x\n",
  102. ret, addr);
  103. }
  104. static uint8_t
  105. sil164_read(struct i2c_client *client, uint8_t addr)
  106. {
  107. uint8_t val;
  108. int ret;
  109. ret = i2c_master_send(client, &addr, sizeof(addr));
  110. if (ret < 0)
  111. goto fail;
  112. ret = i2c_master_recv(client, &val, sizeof(val));
  113. if (ret < 0)
  114. goto fail;
  115. return val;
  116. fail:
  117. sil164_err(client, "Error %d reading from subaddress 0x%x\n",
  118. ret, addr);
  119. return 0;
  120. }
  121. static void
  122. sil164_save_state(struct i2c_client *client, uint8_t *state)
  123. {
  124. int i;
  125. for (i = 0x8; i <= 0xe; i++)
  126. state[i] = sil164_read(client, i);
  127. }
  128. static void
  129. sil164_restore_state(struct i2c_client *client, uint8_t *state)
  130. {
  131. int i;
  132. for (i = 0x8; i <= 0xe; i++)
  133. sil164_write(client, i, state[i]);
  134. }
  135. static void
  136. sil164_set_power_state(struct i2c_client *client, bool on)
  137. {
  138. uint8_t control0 = sil164_read(client, SIL164_CONTROL0);
  139. if (on)
  140. control0 |= SIL164_CONTROL0_POWER_ON;
  141. else
  142. control0 &= ~SIL164_CONTROL0_POWER_ON;
  143. sil164_write(client, SIL164_CONTROL0, control0);
  144. }
  145. static void
  146. sil164_init_state(struct i2c_client *client,
  147. struct sil164_encoder_params *config,
  148. bool duallink)
  149. {
  150. sil164_write(client, SIL164_CONTROL0,
  151. SIL164_CONTROL0_HSYNC_ON |
  152. SIL164_CONTROL0_VSYNC_ON |
  153. (config->input_edge ? SIL164_CONTROL0_EDGE_RISING : 0) |
  154. (config->input_width ? SIL164_CONTROL0_INPUT_24BIT : 0) |
  155. (config->input_dual ? SIL164_CONTROL0_DUAL_EDGE : 0));
  156. sil164_write(client, SIL164_DETECT,
  157. SIL164_DETECT_INTR_STAT |
  158. SIL164_DETECT_OUT_MODE_RECEIVER);
  159. sil164_write(client, SIL164_CONTROL1,
  160. (config->input_skew ? SIL164_CONTROL1_DESKEW_ENABLE : 0) |
  161. (((config->input_skew + 4) & 0x7)
  162. << SIL164_CONTROL1_DESKEW_INCR_SHIFT));
  163. sil164_write(client, SIL164_CONTROL2,
  164. SIL164_CONTROL2_SYNC_CONT |
  165. (config->pll_filter ? 0 : SIL164_CONTROL2_FILTER_ENABLE) |
  166. (4 << SIL164_CONTROL2_FILTER_SETTING_SHIFT));
  167. sil164_write(client, SIL164_PLLZONE, 0);
  168. if (duallink)
  169. sil164_write(client, SIL164_DUALLINK,
  170. SIL164_DUALLINK_ENABLE |
  171. (((config->duallink_skew + 4) & 0x7)
  172. << SIL164_DUALLINK_SKEW_SHIFT));
  173. else
  174. sil164_write(client, SIL164_DUALLINK, 0);
  175. }
  176. /* DRM encoder functions */
  177. static void
  178. sil164_encoder_set_config(struct drm_encoder *encoder, void *params)
  179. {
  180. struct sil164_priv *priv = to_sil164_priv(encoder);
  181. priv->config = *(struct sil164_encoder_params *)params;
  182. }
  183. static void
  184. sil164_encoder_dpms(struct drm_encoder *encoder, int mode)
  185. {
  186. struct sil164_priv *priv = to_sil164_priv(encoder);
  187. bool on = (mode == DRM_MODE_DPMS_ON);
  188. bool duallink = (on && encoder->crtc->mode.clock > 165000);
  189. sil164_set_power_state(drm_i2c_encoder_get_client(encoder), on);
  190. if (priv->duallink_slave)
  191. sil164_set_power_state(priv->duallink_slave, duallink);
  192. }
  193. static void
  194. sil164_encoder_save(struct drm_encoder *encoder)
  195. {
  196. struct sil164_priv *priv = to_sil164_priv(encoder);
  197. sil164_save_state(drm_i2c_encoder_get_client(encoder),
  198. priv->saved_state);
  199. if (priv->duallink_slave)
  200. sil164_save_state(priv->duallink_slave,
  201. priv->saved_slave_state);
  202. }
  203. static void
  204. sil164_encoder_restore(struct drm_encoder *encoder)
  205. {
  206. struct sil164_priv *priv = to_sil164_priv(encoder);
  207. sil164_restore_state(drm_i2c_encoder_get_client(encoder),
  208. priv->saved_state);
  209. if (priv->duallink_slave)
  210. sil164_restore_state(priv->duallink_slave,
  211. priv->saved_slave_state);
  212. }
  213. static int
  214. sil164_encoder_mode_valid(struct drm_encoder *encoder,
  215. struct drm_display_mode *mode)
  216. {
  217. struct sil164_priv *priv = to_sil164_priv(encoder);
  218. if (mode->clock < 32000)
  219. return MODE_CLOCK_LOW;
  220. if (mode->clock > 330000 ||
  221. (mode->clock > 165000 && !priv->duallink_slave))
  222. return MODE_CLOCK_HIGH;
  223. return MODE_OK;
  224. }
  225. static void
  226. sil164_encoder_mode_set(struct drm_encoder *encoder,
  227. struct drm_display_mode *mode,
  228. struct drm_display_mode *adjusted_mode)
  229. {
  230. struct sil164_priv *priv = to_sil164_priv(encoder);
  231. bool duallink = adjusted_mode->clock > 165000;
  232. sil164_init_state(drm_i2c_encoder_get_client(encoder),
  233. &priv->config, duallink);
  234. if (priv->duallink_slave)
  235. sil164_init_state(priv->duallink_slave,
  236. &priv->config, duallink);
  237. sil164_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  238. }
  239. static enum drm_connector_status
  240. sil164_encoder_detect(struct drm_encoder *encoder,
  241. struct drm_connector *connector)
  242. {
  243. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  244. if (sil164_read(client, SIL164_DETECT) & SIL164_DETECT_HOTPLUG_STAT)
  245. return connector_status_connected;
  246. else
  247. return connector_status_disconnected;
  248. }
  249. static int
  250. sil164_encoder_get_modes(struct drm_encoder *encoder,
  251. struct drm_connector *connector)
  252. {
  253. return 0;
  254. }
  255. static int
  256. sil164_encoder_create_resources(struct drm_encoder *encoder,
  257. struct drm_connector *connector)
  258. {
  259. return 0;
  260. }
  261. static int
  262. sil164_encoder_set_property(struct drm_encoder *encoder,
  263. struct drm_connector *connector,
  264. struct drm_property *property,
  265. uint64_t val)
  266. {
  267. return 0;
  268. }
  269. static void
  270. sil164_encoder_destroy(struct drm_encoder *encoder)
  271. {
  272. struct sil164_priv *priv = to_sil164_priv(encoder);
  273. i2c_unregister_device(priv->duallink_slave);
  274. kfree(priv);
  275. drm_i2c_encoder_destroy(encoder);
  276. }
  277. static const struct drm_encoder_slave_funcs sil164_encoder_funcs = {
  278. .set_config = sil164_encoder_set_config,
  279. .destroy = sil164_encoder_destroy,
  280. .dpms = sil164_encoder_dpms,
  281. .save = sil164_encoder_save,
  282. .restore = sil164_encoder_restore,
  283. .mode_valid = sil164_encoder_mode_valid,
  284. .mode_set = sil164_encoder_mode_set,
  285. .detect = sil164_encoder_detect,
  286. .get_modes = sil164_encoder_get_modes,
  287. .create_resources = sil164_encoder_create_resources,
  288. .set_property = sil164_encoder_set_property,
  289. };
  290. /* I2C driver functions */
  291. static int
  292. sil164_probe(struct i2c_client *client)
  293. {
  294. int vendor = sil164_read(client, SIL164_VENDOR_HI) << 8 |
  295. sil164_read(client, SIL164_VENDOR_LO);
  296. int device = sil164_read(client, SIL164_DEVICE_HI) << 8 |
  297. sil164_read(client, SIL164_DEVICE_LO);
  298. int rev = sil164_read(client, SIL164_REVISION);
  299. if (vendor != 0x1 || device != 0x6) {
  300. sil164_dbg(client, "Unknown device %x:%x.%x\n",
  301. vendor, device, rev);
  302. return -ENODEV;
  303. }
  304. sil164_info(client, "Detected device %x:%x.%x\n",
  305. vendor, device, rev);
  306. return 0;
  307. }
  308. static struct i2c_client *
  309. sil164_detect_slave(struct i2c_client *client)
  310. {
  311. struct i2c_adapter *adap = client->adapter;
  312. struct i2c_msg msg = {
  313. .addr = SIL164_I2C_ADDR_SLAVE,
  314. .len = 0,
  315. };
  316. const struct i2c_board_info info = {
  317. I2C_BOARD_INFO("sil164", SIL164_I2C_ADDR_SLAVE)
  318. };
  319. if (i2c_transfer(adap, &msg, 1) != 1) {
  320. sil164_dbg(adap, "No dual-link slave found.");
  321. return NULL;
  322. }
  323. return i2c_new_client_device(adap, &info);
  324. }
  325. static int
  326. sil164_encoder_init(struct i2c_client *client,
  327. struct drm_device *dev,
  328. struct drm_encoder_slave *encoder)
  329. {
  330. struct sil164_priv *priv;
  331. struct i2c_client *slave_client;
  332. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  333. if (!priv)
  334. return -ENOMEM;
  335. encoder->slave_priv = priv;
  336. encoder->slave_funcs = &sil164_encoder_funcs;
  337. slave_client = sil164_detect_slave(client);
  338. if (!IS_ERR(slave_client))
  339. priv->duallink_slave = slave_client;
  340. return 0;
  341. }
  342. static const struct i2c_device_id sil164_ids[] = {
  343. { "sil164", 0 },
  344. { }
  345. };
  346. MODULE_DEVICE_TABLE(i2c, sil164_ids);
  347. static struct drm_i2c_encoder_driver sil164_driver = {
  348. .i2c_driver = {
  349. .probe = sil164_probe,
  350. .driver = {
  351. .name = "sil164",
  352. },
  353. .id_table = sil164_ids,
  354. },
  355. .encoder_init = sil164_encoder_init,
  356. };
  357. /* Module initialization */
  358. static int __init
  359. sil164_init(void)
  360. {
  361. return drm_i2c_encoder_register(THIS_MODULE, &sil164_driver);
  362. }
  363. static void __exit
  364. sil164_exit(void)
  365. {
  366. drm_i2c_encoder_unregister(&sil164_driver);
  367. }
  368. MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
  369. MODULE_DESCRIPTION("Silicon Image sil164 TMDS transmitter driver");
  370. MODULE_LICENSE("GPL and additional rights");
  371. module_init(sil164_init);
  372. module_exit(sil164_exit);