sil164_drv.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 bool
  213. sil164_encoder_mode_fixup(struct drm_encoder *encoder,
  214. const struct drm_display_mode *mode,
  215. struct drm_display_mode *adjusted_mode)
  216. {
  217. return true;
  218. }
  219. static int
  220. sil164_encoder_mode_valid(struct drm_encoder *encoder,
  221. struct drm_display_mode *mode)
  222. {
  223. struct sil164_priv *priv = to_sil164_priv(encoder);
  224. if (mode->clock < 32000)
  225. return MODE_CLOCK_LOW;
  226. if (mode->clock > 330000 ||
  227. (mode->clock > 165000 && !priv->duallink_slave))
  228. return MODE_CLOCK_HIGH;
  229. return MODE_OK;
  230. }
  231. static void
  232. sil164_encoder_mode_set(struct drm_encoder *encoder,
  233. struct drm_display_mode *mode,
  234. struct drm_display_mode *adjusted_mode)
  235. {
  236. struct sil164_priv *priv = to_sil164_priv(encoder);
  237. bool duallink = adjusted_mode->clock > 165000;
  238. sil164_init_state(drm_i2c_encoder_get_client(encoder),
  239. &priv->config, duallink);
  240. if (priv->duallink_slave)
  241. sil164_init_state(priv->duallink_slave,
  242. &priv->config, duallink);
  243. sil164_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  244. }
  245. static enum drm_connector_status
  246. sil164_encoder_detect(struct drm_encoder *encoder,
  247. struct drm_connector *connector)
  248. {
  249. struct i2c_client *client = drm_i2c_encoder_get_client(encoder);
  250. if (sil164_read(client, SIL164_DETECT) & SIL164_DETECT_HOTPLUG_STAT)
  251. return connector_status_connected;
  252. else
  253. return connector_status_disconnected;
  254. }
  255. static int
  256. sil164_encoder_get_modes(struct drm_encoder *encoder,
  257. struct drm_connector *connector)
  258. {
  259. return 0;
  260. }
  261. static int
  262. sil164_encoder_create_resources(struct drm_encoder *encoder,
  263. struct drm_connector *connector)
  264. {
  265. return 0;
  266. }
  267. static int
  268. sil164_encoder_set_property(struct drm_encoder *encoder,
  269. struct drm_connector *connector,
  270. struct drm_property *property,
  271. uint64_t val)
  272. {
  273. return 0;
  274. }
  275. static void
  276. sil164_encoder_destroy(struct drm_encoder *encoder)
  277. {
  278. struct sil164_priv *priv = to_sil164_priv(encoder);
  279. if (priv->duallink_slave)
  280. i2c_unregister_device(priv->duallink_slave);
  281. kfree(priv);
  282. drm_i2c_encoder_destroy(encoder);
  283. }
  284. static struct drm_encoder_slave_funcs sil164_encoder_funcs = {
  285. .set_config = sil164_encoder_set_config,
  286. .destroy = sil164_encoder_destroy,
  287. .dpms = sil164_encoder_dpms,
  288. .save = sil164_encoder_save,
  289. .restore = sil164_encoder_restore,
  290. .mode_fixup = sil164_encoder_mode_fixup,
  291. .mode_valid = sil164_encoder_mode_valid,
  292. .mode_set = sil164_encoder_mode_set,
  293. .detect = sil164_encoder_detect,
  294. .get_modes = sil164_encoder_get_modes,
  295. .create_resources = sil164_encoder_create_resources,
  296. .set_property = sil164_encoder_set_property,
  297. };
  298. /* I2C driver functions */
  299. static int
  300. sil164_probe(struct i2c_client *client, const struct i2c_device_id *id)
  301. {
  302. int vendor = sil164_read(client, SIL164_VENDOR_HI) << 8 |
  303. sil164_read(client, SIL164_VENDOR_LO);
  304. int device = sil164_read(client, SIL164_DEVICE_HI) << 8 |
  305. sil164_read(client, SIL164_DEVICE_LO);
  306. int rev = sil164_read(client, SIL164_REVISION);
  307. if (vendor != 0x1 || device != 0x6) {
  308. sil164_dbg(client, "Unknown device %x:%x.%x\n",
  309. vendor, device, rev);
  310. return -ENODEV;
  311. }
  312. sil164_info(client, "Detected device %x:%x.%x\n",
  313. vendor, device, rev);
  314. return 0;
  315. }
  316. static int
  317. sil164_remove(struct i2c_client *client)
  318. {
  319. return 0;
  320. }
  321. static struct i2c_client *
  322. sil164_detect_slave(struct i2c_client *client)
  323. {
  324. struct i2c_adapter *adap = client->adapter;
  325. struct i2c_msg msg = {
  326. .addr = SIL164_I2C_ADDR_SLAVE,
  327. .len = 0,
  328. };
  329. const struct i2c_board_info info = {
  330. I2C_BOARD_INFO("sil164", SIL164_I2C_ADDR_SLAVE)
  331. };
  332. if (i2c_transfer(adap, &msg, 1) != 1) {
  333. sil164_dbg(adap, "No dual-link slave found.");
  334. return NULL;
  335. }
  336. return i2c_new_device(adap, &info);
  337. }
  338. static int
  339. sil164_encoder_init(struct i2c_client *client,
  340. struct drm_device *dev,
  341. struct drm_encoder_slave *encoder)
  342. {
  343. struct sil164_priv *priv;
  344. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  345. if (!priv)
  346. return -ENOMEM;
  347. encoder->slave_priv = priv;
  348. encoder->slave_funcs = &sil164_encoder_funcs;
  349. priv->duallink_slave = sil164_detect_slave(client);
  350. return 0;
  351. }
  352. static struct i2c_device_id sil164_ids[] = {
  353. { "sil164", 0 },
  354. { }
  355. };
  356. MODULE_DEVICE_TABLE(i2c, sil164_ids);
  357. static struct drm_i2c_encoder_driver sil164_driver = {
  358. .i2c_driver = {
  359. .probe = sil164_probe,
  360. .remove = sil164_remove,
  361. .driver = {
  362. .name = "sil164",
  363. },
  364. .id_table = sil164_ids,
  365. },
  366. .encoder_init = sil164_encoder_init,
  367. };
  368. /* Module initialization */
  369. static int __init
  370. sil164_init(void)
  371. {
  372. return drm_i2c_encoder_register(THIS_MODULE, &sil164_driver);
  373. }
  374. static void __exit
  375. sil164_exit(void)
  376. {
  377. drm_i2c_encoder_unregister(&sil164_driver);
  378. }
  379. MODULE_AUTHOR("Francisco Jerez <currojerez@riseup.net>");
  380. MODULE_DESCRIPTION("Silicon Image sil164 TMDS transmitter driver");
  381. MODULE_LICENSE("GPL and additional rights");
  382. module_init(sil164_init);
  383. module_exit(sil164_exit);