megachips-stdpxxxx-ge-b850v3-fw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Driver for MegaChips STDP4028 with GE B850v3 firmware (LVDS-DP)
  3. * Driver for MegaChips STDP2690 with GE B850v3 firmware (DP-DP++)
  4. * Copyright (c) 2017, Collabora Ltd.
  5. * Copyright (c) 2017, General Electric Company
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. * This driver creates a drm_bridge and a drm_connector for the LVDS to DP++
  16. * display bridge of the GE B850v3. There are two physical bridges on the video
  17. * signal pipeline: a STDP4028(LVDS to DP) and a STDP2690(DP to DP++). The
  18. * physical bridges are automatically configured by the input video signal, and
  19. * the driver has no access to the video processing pipeline. The driver is
  20. * only needed to read EDID from the STDP2690 and to handle HPD events from the
  21. * STDP4028. The driver communicates with both bridges over i2c. The video
  22. * signal pipeline is as follows:
  23. *
  24. * Host -> LVDS|--(STDP4028)--|DP -> DP|--(STDP2690)--|DP++ -> Video output
  25. *
  26. */
  27. #include <linux/gpio.h>
  28. #include <linux/i2c.h>
  29. #include <linux/module.h>
  30. #include <linux/of.h>
  31. #include <drm/drm_atomic.h>
  32. #include <drm/drm_atomic_helper.h>
  33. #include <drm/drm_crtc_helper.h>
  34. #include <drm/drm_edid.h>
  35. #include <drm/drmP.h>
  36. #define EDID_EXT_BLOCK_CNT 0x7E
  37. #define STDP4028_IRQ_OUT_CONF_REG 0x02
  38. #define STDP4028_DPTX_IRQ_EN_REG 0x3C
  39. #define STDP4028_DPTX_IRQ_STS_REG 0x3D
  40. #define STDP4028_DPTX_STS_REG 0x3E
  41. #define STDP4028_DPTX_DP_IRQ_EN 0x1000
  42. #define STDP4028_DPTX_HOTPLUG_IRQ_EN 0x0400
  43. #define STDP4028_DPTX_LINK_CH_IRQ_EN 0x2000
  44. #define STDP4028_DPTX_IRQ_CONFIG \
  45. (STDP4028_DPTX_LINK_CH_IRQ_EN | STDP4028_DPTX_HOTPLUG_IRQ_EN)
  46. #define STDP4028_DPTX_HOTPLUG_STS 0x0200
  47. #define STDP4028_DPTX_LINK_STS 0x1000
  48. #define STDP4028_CON_STATE_CONNECTED \
  49. (STDP4028_DPTX_HOTPLUG_STS | STDP4028_DPTX_LINK_STS)
  50. #define STDP4028_DPTX_HOTPLUG_CH_STS 0x0400
  51. #define STDP4028_DPTX_LINK_CH_STS 0x2000
  52. #define STDP4028_DPTX_IRQ_CLEAR \
  53. (STDP4028_DPTX_LINK_CH_STS | STDP4028_DPTX_HOTPLUG_CH_STS)
  54. static DEFINE_MUTEX(ge_b850v3_lvds_dev_mutex);
  55. struct ge_b850v3_lvds {
  56. struct drm_connector connector;
  57. struct drm_bridge bridge;
  58. struct i2c_client *stdp4028_i2c;
  59. struct i2c_client *stdp2690_i2c;
  60. struct edid *edid;
  61. };
  62. static struct ge_b850v3_lvds *ge_b850v3_lvds_ptr;
  63. static u8 *stdp2690_get_edid(struct i2c_client *client)
  64. {
  65. struct i2c_adapter *adapter = client->adapter;
  66. unsigned char start = 0x00;
  67. unsigned int total_size;
  68. u8 *block = kmalloc(EDID_LENGTH, GFP_KERNEL);
  69. struct i2c_msg msgs[] = {
  70. {
  71. .addr = client->addr,
  72. .flags = 0,
  73. .len = 1,
  74. .buf = &start,
  75. }, {
  76. .addr = client->addr,
  77. .flags = I2C_M_RD,
  78. .len = EDID_LENGTH,
  79. .buf = block,
  80. }
  81. };
  82. if (!block)
  83. return NULL;
  84. if (i2c_transfer(adapter, msgs, 2) != 2) {
  85. DRM_ERROR("Unable to read EDID.\n");
  86. goto err;
  87. }
  88. if (!drm_edid_block_valid(block, 0, false, NULL)) {
  89. DRM_ERROR("Invalid EDID data\n");
  90. goto err;
  91. }
  92. total_size = (block[EDID_EXT_BLOCK_CNT] + 1) * EDID_LENGTH;
  93. if (total_size > EDID_LENGTH) {
  94. kfree(block);
  95. block = kmalloc(total_size, GFP_KERNEL);
  96. if (!block)
  97. return NULL;
  98. /* Yes, read the entire buffer, and do not skip the first
  99. * EDID_LENGTH bytes.
  100. */
  101. start = 0x00;
  102. msgs[1].len = total_size;
  103. msgs[1].buf = block;
  104. if (i2c_transfer(adapter, msgs, 2) != 2) {
  105. DRM_ERROR("Unable to read EDID extension blocks.\n");
  106. goto err;
  107. }
  108. if (!drm_edid_block_valid(block, 1, false, NULL)) {
  109. DRM_ERROR("Invalid EDID data\n");
  110. goto err;
  111. }
  112. }
  113. return block;
  114. err:
  115. kfree(block);
  116. return NULL;
  117. }
  118. static int ge_b850v3_lvds_get_modes(struct drm_connector *connector)
  119. {
  120. struct i2c_client *client;
  121. int num_modes = 0;
  122. client = ge_b850v3_lvds_ptr->stdp2690_i2c;
  123. kfree(ge_b850v3_lvds_ptr->edid);
  124. ge_b850v3_lvds_ptr->edid = (struct edid *)stdp2690_get_edid(client);
  125. if (ge_b850v3_lvds_ptr->edid) {
  126. drm_mode_connector_update_edid_property(connector,
  127. ge_b850v3_lvds_ptr->edid);
  128. num_modes = drm_add_edid_modes(connector,
  129. ge_b850v3_lvds_ptr->edid);
  130. }
  131. return num_modes;
  132. }
  133. static enum drm_mode_status ge_b850v3_lvds_mode_valid(
  134. struct drm_connector *connector, struct drm_display_mode *mode)
  135. {
  136. return MODE_OK;
  137. }
  138. static const struct
  139. drm_connector_helper_funcs ge_b850v3_lvds_connector_helper_funcs = {
  140. .get_modes = ge_b850v3_lvds_get_modes,
  141. .mode_valid = ge_b850v3_lvds_mode_valid,
  142. };
  143. static enum drm_connector_status ge_b850v3_lvds_detect(
  144. struct drm_connector *connector, bool force)
  145. {
  146. struct i2c_client *stdp4028_i2c =
  147. ge_b850v3_lvds_ptr->stdp4028_i2c;
  148. s32 link_state;
  149. link_state = i2c_smbus_read_word_data(stdp4028_i2c,
  150. STDP4028_DPTX_STS_REG);
  151. if (link_state == STDP4028_CON_STATE_CONNECTED)
  152. return connector_status_connected;
  153. if (link_state == 0)
  154. return connector_status_disconnected;
  155. return connector_status_unknown;
  156. }
  157. static const struct drm_connector_funcs ge_b850v3_lvds_connector_funcs = {
  158. .fill_modes = drm_helper_probe_single_connector_modes,
  159. .detect = ge_b850v3_lvds_detect,
  160. .destroy = drm_connector_cleanup,
  161. .reset = drm_atomic_helper_connector_reset,
  162. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  163. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  164. };
  165. static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
  166. {
  167. struct i2c_client *stdp4028_i2c
  168. = ge_b850v3_lvds_ptr->stdp4028_i2c;
  169. i2c_smbus_write_word_data(stdp4028_i2c,
  170. STDP4028_DPTX_IRQ_STS_REG,
  171. STDP4028_DPTX_IRQ_CLEAR);
  172. if (ge_b850v3_lvds_ptr->connector.dev)
  173. drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->connector.dev);
  174. return IRQ_HANDLED;
  175. }
  176. static int ge_b850v3_lvds_attach(struct drm_bridge *bridge)
  177. {
  178. struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
  179. struct i2c_client *stdp4028_i2c
  180. = ge_b850v3_lvds_ptr->stdp4028_i2c;
  181. int ret;
  182. if (!bridge->encoder) {
  183. DRM_ERROR("Parent encoder object not found");
  184. return -ENODEV;
  185. }
  186. connector->polled = DRM_CONNECTOR_POLL_HPD;
  187. drm_connector_helper_add(connector,
  188. &ge_b850v3_lvds_connector_helper_funcs);
  189. ret = drm_connector_init(bridge->dev, connector,
  190. &ge_b850v3_lvds_connector_funcs,
  191. DRM_MODE_CONNECTOR_DisplayPort);
  192. if (ret) {
  193. DRM_ERROR("Failed to initialize connector with drm\n");
  194. return ret;
  195. }
  196. ret = drm_mode_connector_attach_encoder(connector, bridge->encoder);
  197. if (ret)
  198. return ret;
  199. /* Configures the bridge to re-enable interrupts after each ack. */
  200. i2c_smbus_write_word_data(stdp4028_i2c,
  201. STDP4028_IRQ_OUT_CONF_REG,
  202. STDP4028_DPTX_DP_IRQ_EN);
  203. /* Enable interrupts */
  204. i2c_smbus_write_word_data(stdp4028_i2c,
  205. STDP4028_DPTX_IRQ_EN_REG,
  206. STDP4028_DPTX_IRQ_CONFIG);
  207. return 0;
  208. }
  209. static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
  210. .attach = ge_b850v3_lvds_attach,
  211. };
  212. static int ge_b850v3_lvds_init(struct device *dev)
  213. {
  214. mutex_lock(&ge_b850v3_lvds_dev_mutex);
  215. if (ge_b850v3_lvds_ptr)
  216. goto success;
  217. ge_b850v3_lvds_ptr = devm_kzalloc(dev,
  218. sizeof(*ge_b850v3_lvds_ptr),
  219. GFP_KERNEL);
  220. if (!ge_b850v3_lvds_ptr) {
  221. mutex_unlock(&ge_b850v3_lvds_dev_mutex);
  222. return -ENOMEM;
  223. }
  224. success:
  225. mutex_unlock(&ge_b850v3_lvds_dev_mutex);
  226. return 0;
  227. }
  228. static void ge_b850v3_lvds_remove(void)
  229. {
  230. mutex_lock(&ge_b850v3_lvds_dev_mutex);
  231. /*
  232. * This check is to avoid both the drivers
  233. * removing the bridge in their remove() function
  234. */
  235. if (!ge_b850v3_lvds_ptr)
  236. goto out;
  237. drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
  238. kfree(ge_b850v3_lvds_ptr->edid);
  239. ge_b850v3_lvds_ptr = NULL;
  240. out:
  241. mutex_unlock(&ge_b850v3_lvds_dev_mutex);
  242. }
  243. static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c,
  244. const struct i2c_device_id *id)
  245. {
  246. struct device *dev = &stdp4028_i2c->dev;
  247. ge_b850v3_lvds_init(dev);
  248. ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
  249. i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
  250. /* drm bridge initialization */
  251. ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs;
  252. ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
  253. drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
  254. /* Clear pending interrupts since power up. */
  255. i2c_smbus_write_word_data(stdp4028_i2c,
  256. STDP4028_DPTX_IRQ_STS_REG,
  257. STDP4028_DPTX_IRQ_CLEAR);
  258. if (!stdp4028_i2c->irq)
  259. return 0;
  260. return devm_request_threaded_irq(&stdp4028_i2c->dev,
  261. stdp4028_i2c->irq, NULL,
  262. ge_b850v3_lvds_irq_handler,
  263. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  264. "ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
  265. }
  266. static int stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
  267. {
  268. ge_b850v3_lvds_remove();
  269. return 0;
  270. }
  271. static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
  272. {"stdp4028_ge_fw", 0},
  273. {},
  274. };
  275. MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
  276. static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
  277. { .compatible = "megachips,stdp4028-ge-b850v3-fw" },
  278. {},
  279. };
  280. MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
  281. static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
  282. .id_table = stdp4028_ge_b850v3_fw_i2c_table,
  283. .probe = stdp4028_ge_b850v3_fw_probe,
  284. .remove = stdp4028_ge_b850v3_fw_remove,
  285. .driver = {
  286. .name = "stdp4028-ge-b850v3-fw",
  287. .of_match_table = stdp4028_ge_b850v3_fw_match,
  288. },
  289. };
  290. static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c,
  291. const struct i2c_device_id *id)
  292. {
  293. struct device *dev = &stdp2690_i2c->dev;
  294. ge_b850v3_lvds_init(dev);
  295. ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
  296. i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
  297. return 0;
  298. }
  299. static int stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
  300. {
  301. ge_b850v3_lvds_remove();
  302. return 0;
  303. }
  304. static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
  305. {"stdp2690_ge_fw", 0},
  306. {},
  307. };
  308. MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
  309. static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
  310. { .compatible = "megachips,stdp2690-ge-b850v3-fw" },
  311. {},
  312. };
  313. MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
  314. static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
  315. .id_table = stdp2690_ge_b850v3_fw_i2c_table,
  316. .probe = stdp2690_ge_b850v3_fw_probe,
  317. .remove = stdp2690_ge_b850v3_fw_remove,
  318. .driver = {
  319. .name = "stdp2690-ge-b850v3-fw",
  320. .of_match_table = stdp2690_ge_b850v3_fw_match,
  321. },
  322. };
  323. static int __init stdpxxxx_ge_b850v3_init(void)
  324. {
  325. int ret;
  326. ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
  327. if (ret)
  328. return ret;
  329. return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
  330. }
  331. module_init(stdpxxxx_ge_b850v3_init);
  332. static void __exit stdpxxxx_ge_b850v3_exit(void)
  333. {
  334. i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
  335. i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
  336. }
  337. module_exit(stdpxxxx_ge_b850v3_exit);
  338. MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
  339. MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
  340. MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
  341. MODULE_LICENSE("GPL v2");