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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. .dpms = drm_atomic_helper_connector_dpms,
  159. .fill_modes = drm_helper_probe_single_connector_modes,
  160. .detect = ge_b850v3_lvds_detect,
  161. .destroy = drm_connector_cleanup,
  162. .reset = drm_atomic_helper_connector_reset,
  163. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  164. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  165. };
  166. static irqreturn_t ge_b850v3_lvds_irq_handler(int irq, void *dev_id)
  167. {
  168. struct i2c_client *stdp4028_i2c
  169. = ge_b850v3_lvds_ptr->stdp4028_i2c;
  170. i2c_smbus_write_word_data(stdp4028_i2c,
  171. STDP4028_DPTX_IRQ_STS_REG,
  172. STDP4028_DPTX_IRQ_CLEAR);
  173. if (ge_b850v3_lvds_ptr->connector.dev)
  174. drm_kms_helper_hotplug_event(ge_b850v3_lvds_ptr->connector.dev);
  175. return IRQ_HANDLED;
  176. }
  177. static int ge_b850v3_lvds_attach(struct drm_bridge *bridge)
  178. {
  179. struct drm_connector *connector = &ge_b850v3_lvds_ptr->connector;
  180. struct i2c_client *stdp4028_i2c
  181. = ge_b850v3_lvds_ptr->stdp4028_i2c;
  182. int ret;
  183. if (!bridge->encoder) {
  184. DRM_ERROR("Parent encoder object not found");
  185. return -ENODEV;
  186. }
  187. connector->polled = DRM_CONNECTOR_POLL_HPD;
  188. drm_connector_helper_add(connector,
  189. &ge_b850v3_lvds_connector_helper_funcs);
  190. ret = drm_connector_init(bridge->dev, connector,
  191. &ge_b850v3_lvds_connector_funcs,
  192. DRM_MODE_CONNECTOR_DisplayPort);
  193. if (ret) {
  194. DRM_ERROR("Failed to initialize connector with drm\n");
  195. return ret;
  196. }
  197. ret = drm_mode_connector_attach_encoder(connector, bridge->encoder);
  198. if (ret)
  199. return ret;
  200. /* Configures the bridge to re-enable interrupts after each ack. */
  201. i2c_smbus_write_word_data(stdp4028_i2c,
  202. STDP4028_IRQ_OUT_CONF_REG,
  203. STDP4028_DPTX_DP_IRQ_EN);
  204. /* Enable interrupts */
  205. i2c_smbus_write_word_data(stdp4028_i2c,
  206. STDP4028_DPTX_IRQ_EN_REG,
  207. STDP4028_DPTX_IRQ_CONFIG);
  208. return 0;
  209. }
  210. static const struct drm_bridge_funcs ge_b850v3_lvds_funcs = {
  211. .attach = ge_b850v3_lvds_attach,
  212. };
  213. static int ge_b850v3_lvds_init(struct device *dev)
  214. {
  215. mutex_lock(&ge_b850v3_lvds_dev_mutex);
  216. if (ge_b850v3_lvds_ptr)
  217. goto success;
  218. ge_b850v3_lvds_ptr = devm_kzalloc(dev,
  219. sizeof(*ge_b850v3_lvds_ptr),
  220. GFP_KERNEL);
  221. if (!ge_b850v3_lvds_ptr) {
  222. mutex_unlock(&ge_b850v3_lvds_dev_mutex);
  223. return -ENOMEM;
  224. }
  225. success:
  226. mutex_unlock(&ge_b850v3_lvds_dev_mutex);
  227. return 0;
  228. }
  229. static void ge_b850v3_lvds_remove(void)
  230. {
  231. mutex_lock(&ge_b850v3_lvds_dev_mutex);
  232. /*
  233. * This check is to avoid both the drivers
  234. * removing the bridge in their remove() function
  235. */
  236. if (!ge_b850v3_lvds_ptr)
  237. goto out;
  238. drm_bridge_remove(&ge_b850v3_lvds_ptr->bridge);
  239. kfree(ge_b850v3_lvds_ptr->edid);
  240. ge_b850v3_lvds_ptr = NULL;
  241. out:
  242. mutex_unlock(&ge_b850v3_lvds_dev_mutex);
  243. }
  244. static int stdp4028_ge_b850v3_fw_probe(struct i2c_client *stdp4028_i2c,
  245. const struct i2c_device_id *id)
  246. {
  247. struct device *dev = &stdp4028_i2c->dev;
  248. ge_b850v3_lvds_init(dev);
  249. ge_b850v3_lvds_ptr->stdp4028_i2c = stdp4028_i2c;
  250. i2c_set_clientdata(stdp4028_i2c, ge_b850v3_lvds_ptr);
  251. /* drm bridge initialization */
  252. ge_b850v3_lvds_ptr->bridge.funcs = &ge_b850v3_lvds_funcs;
  253. ge_b850v3_lvds_ptr->bridge.of_node = dev->of_node;
  254. drm_bridge_add(&ge_b850v3_lvds_ptr->bridge);
  255. /* Clear pending interrupts since power up. */
  256. i2c_smbus_write_word_data(stdp4028_i2c,
  257. STDP4028_DPTX_IRQ_STS_REG,
  258. STDP4028_DPTX_IRQ_CLEAR);
  259. if (!stdp4028_i2c->irq)
  260. return 0;
  261. return devm_request_threaded_irq(&stdp4028_i2c->dev,
  262. stdp4028_i2c->irq, NULL,
  263. ge_b850v3_lvds_irq_handler,
  264. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  265. "ge-b850v3-lvds-dp", ge_b850v3_lvds_ptr);
  266. }
  267. static int stdp4028_ge_b850v3_fw_remove(struct i2c_client *stdp4028_i2c)
  268. {
  269. ge_b850v3_lvds_remove();
  270. return 0;
  271. }
  272. static const struct i2c_device_id stdp4028_ge_b850v3_fw_i2c_table[] = {
  273. {"stdp4028_ge_fw", 0},
  274. {},
  275. };
  276. MODULE_DEVICE_TABLE(i2c, stdp4028_ge_b850v3_fw_i2c_table);
  277. static const struct of_device_id stdp4028_ge_b850v3_fw_match[] = {
  278. { .compatible = "megachips,stdp4028-ge-b850v3-fw" },
  279. {},
  280. };
  281. MODULE_DEVICE_TABLE(of, stdp4028_ge_b850v3_fw_match);
  282. static struct i2c_driver stdp4028_ge_b850v3_fw_driver = {
  283. .id_table = stdp4028_ge_b850v3_fw_i2c_table,
  284. .probe = stdp4028_ge_b850v3_fw_probe,
  285. .remove = stdp4028_ge_b850v3_fw_remove,
  286. .driver = {
  287. .name = "stdp4028-ge-b850v3-fw",
  288. .of_match_table = stdp4028_ge_b850v3_fw_match,
  289. },
  290. };
  291. static int stdp2690_ge_b850v3_fw_probe(struct i2c_client *stdp2690_i2c,
  292. const struct i2c_device_id *id)
  293. {
  294. struct device *dev = &stdp2690_i2c->dev;
  295. ge_b850v3_lvds_init(dev);
  296. ge_b850v3_lvds_ptr->stdp2690_i2c = stdp2690_i2c;
  297. i2c_set_clientdata(stdp2690_i2c, ge_b850v3_lvds_ptr);
  298. return 0;
  299. }
  300. static int stdp2690_ge_b850v3_fw_remove(struct i2c_client *stdp2690_i2c)
  301. {
  302. ge_b850v3_lvds_remove();
  303. return 0;
  304. }
  305. static const struct i2c_device_id stdp2690_ge_b850v3_fw_i2c_table[] = {
  306. {"stdp2690_ge_fw", 0},
  307. {},
  308. };
  309. MODULE_DEVICE_TABLE(i2c, stdp2690_ge_b850v3_fw_i2c_table);
  310. static const struct of_device_id stdp2690_ge_b850v3_fw_match[] = {
  311. { .compatible = "megachips,stdp2690-ge-b850v3-fw" },
  312. {},
  313. };
  314. MODULE_DEVICE_TABLE(of, stdp2690_ge_b850v3_fw_match);
  315. static struct i2c_driver stdp2690_ge_b850v3_fw_driver = {
  316. .id_table = stdp2690_ge_b850v3_fw_i2c_table,
  317. .probe = stdp2690_ge_b850v3_fw_probe,
  318. .remove = stdp2690_ge_b850v3_fw_remove,
  319. .driver = {
  320. .name = "stdp2690-ge-b850v3-fw",
  321. .of_match_table = stdp2690_ge_b850v3_fw_match,
  322. },
  323. };
  324. static int __init stdpxxxx_ge_b850v3_init(void)
  325. {
  326. int ret;
  327. ret = i2c_add_driver(&stdp4028_ge_b850v3_fw_driver);
  328. if (ret)
  329. return ret;
  330. return i2c_add_driver(&stdp2690_ge_b850v3_fw_driver);
  331. }
  332. module_init(stdpxxxx_ge_b850v3_init);
  333. static void __exit stdpxxxx_ge_b850v3_exit(void)
  334. {
  335. i2c_del_driver(&stdp2690_ge_b850v3_fw_driver);
  336. i2c_del_driver(&stdp4028_ge_b850v3_fw_driver);
  337. }
  338. module_exit(stdpxxxx_ge_b850v3_exit);
  339. MODULE_AUTHOR("Peter Senna Tschudin <peter.senna@collabora.com>");
  340. MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk>");
  341. MODULE_DESCRIPTION("GE LVDS to DP++ display bridge)");
  342. MODULE_LICENSE("GPL v2");