sil164_drv.c 12 KB

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