sil164_drv.c 12 KB

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