altmode-glink.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020-2021 The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "altmode-glink: %s: " fmt, __func__
  6. #include <linux/debugfs.h>
  7. #include <linux/device.h>
  8. #include <linux/idr.h>
  9. #include <linux/ipc_logging.h>
  10. #include <linux/ktime.h>
  11. #include <linux/module.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include <linux/soc/qcom/altmode-glink.h>
  16. #include <linux/soc/qcom/pmic_glink.h>
  17. #define MSG_OWNER_USBC_PAN 32780
  18. #define MSG_TYPE_REQ_RESP 1
  19. #define NOTIFY_PAYLOAD_SIZE 16
  20. #define USBC_WRITE_BUFFER_SIZE 8
  21. #define USBC_CMD_WRITE_REQ 0x15
  22. #define USBC_NOTIFY_IND 0x16
  23. #define MAX_NUM_PORTS 4
  24. #define NUM_LOG_PAGES 10
  25. #define IDR_KEY_GEN(svid, ind) (((svid) << 8) | (ind))
  26. #define IDR_KEY(client) \
  27. IDR_KEY_GEN((client)->data.svid, (client)->port_index)
  28. #define altmode_dbg(fmt, ...) \
  29. do { \
  30. ipc_log_string(altmode_ipc_log, fmt, ##__VA_ARGS__); \
  31. pr_debug(fmt, ##__VA_ARGS__); \
  32. } while (0)
  33. struct usbc_notify_ind_msg {
  34. struct pmic_glink_hdr hdr;
  35. u8 payload[NOTIFY_PAYLOAD_SIZE];
  36. u32 reserved;
  37. };
  38. struct usbc_write_buffer_req_msg {
  39. struct pmic_glink_hdr hdr;
  40. u8 buf[USBC_WRITE_BUFFER_SIZE];
  41. u32 reserved;
  42. };
  43. /**
  44. * struct altmode_dev
  45. * Definition of an altmode device.
  46. *
  47. * @dev: Altmode parent device for all client devices
  48. * @pgclient: PMIC GLINK client to talk to remote subsystem
  49. * @client_idr: idr list for altmode clients
  50. * @client_lock: mutex protecting changes to client_idr
  51. * @d_node: Linked list node to string together multiple amdev's
  52. * @client_list: Linked list head keeping track of this device's clients
  53. * @pan_en_sent: Flag to ensure PAN Enable msg is sent only once
  54. * @send_pan_en_work: To schedule the sending of the PAN Enable message
  55. * @send_pan_ack_work: To schedule the sending of the PAN Ack message
  56. * @debugfs_dir: Dentry for debugfs directory "altmode"
  57. * @response_received: To detect remote subsystem response failures
  58. * @ack_port_index: Port index to ack for a nonexistent client
  59. */
  60. struct altmode_dev {
  61. struct device *dev;
  62. struct pmic_glink_client *pgclient;
  63. struct idr client_idr;
  64. struct mutex client_lock;
  65. struct list_head d_node;
  66. struct list_head client_list;
  67. atomic_t pan_en_sent;
  68. struct delayed_work send_pan_en_work;
  69. struct delayed_work send_pan_ack_work;
  70. struct dentry *debugfs_dir;
  71. struct completion response_received;
  72. u8 ack_port_index;
  73. };
  74. /**
  75. * struct altmode_client
  76. * Definition of a client of an altmode device.
  77. *
  78. * @amdev: Parent altmode device of this client
  79. * @data: Supplied by client driver during registration
  80. * @c_node: Linked list node for parent altmode device's client list
  81. * @port_index: Type-C port index assigned by remote subystem
  82. *
  83. * Note: The following members are for internal use only, clients should not
  84. * use them.
  85. *
  86. * @client_cb_work: Work to run client callback
  87. * @msg: Latest notify msg stashed to be retrieved in cb_work
  88. */
  89. struct altmode_client {
  90. struct altmode_dev *amdev;
  91. struct altmode_client_data data;
  92. struct list_head c_node;
  93. u8 port_index;
  94. struct work_struct client_cb_work;
  95. u8 msg[NOTIFY_PAYLOAD_SIZE];
  96. };
  97. /**
  98. * struct probe_notify_node
  99. * Linked list node to keep track of altmode clients who, by design,
  100. * register with the altmode framework before altmode probes.
  101. *
  102. * @node: Linked list node for probe_notify_list
  103. * @amdev_node: device_node of the altmode device of the client
  104. * @cb: Client's probe completion callback function
  105. * @priv: Pointer to client's driver data struct
  106. */
  107. struct probe_notify_node {
  108. struct list_head node;
  109. struct device_node *amdev_node;
  110. void (*cb)(void *priv);
  111. void *priv;
  112. };
  113. /**
  114. * struct list_head probe_notify_list
  115. * List of altmode clients that register to get notified upon probe
  116. * completion.
  117. */
  118. static LIST_HEAD(probe_notify_list);
  119. static DEFINE_MUTEX(notify_lock);
  120. static void altmode_send_pan_ack(struct work_struct *work);
  121. static void *altmode_ipc_log;
  122. static struct altmode_dev *to_altmode_device(struct device_node *amdev_node)
  123. {
  124. struct platform_device *altmode_pdev;
  125. altmode_pdev = of_find_device_by_node(amdev_node);
  126. if (altmode_pdev)
  127. return platform_get_drvdata(altmode_pdev);
  128. return NULL;
  129. }
  130. #define ALTMODE_WAIT_MS 1000
  131. static int altmode_write(struct altmode_dev *amdev, void *data, size_t len)
  132. {
  133. int rc;
  134. reinit_completion(&amdev->response_received);
  135. rc = pmic_glink_write(amdev->pgclient, data, len);
  136. if (!rc) {
  137. rc = wait_for_completion_timeout(&amdev->response_received,
  138. msecs_to_jiffies(ALTMODE_WAIT_MS));
  139. rc = rc ? 0 : -ETIMEDOUT;
  140. }
  141. if (rc)
  142. pr_err("Error in sending message: %d\n", rc);
  143. return rc;
  144. }
  145. static int __altmode_send_data(struct altmode_dev *amdev, void *data,
  146. size_t len)
  147. {
  148. struct usbc_write_buffer_req_msg msg = { { 0 } };
  149. if (len > sizeof(msg.buf)) {
  150. pr_err("len %zu exceeds msg buf's size: %zu\n",
  151. len, USBC_WRITE_BUFFER_SIZE);
  152. return -EINVAL;
  153. }
  154. msg.hdr.owner = MSG_OWNER_USBC_PAN;
  155. msg.hdr.type = MSG_TYPE_REQ_RESP;
  156. msg.hdr.opcode = USBC_CMD_WRITE_REQ;
  157. memcpy(msg.buf, data, len);
  158. return altmode_write(amdev, &msg, sizeof(msg));
  159. }
  160. /**
  161. * altmode_register_notifier()
  162. * Register to be notified when altmode device probes.
  163. *
  164. * @client_dev: Client device pointer
  165. * @cb: Callback function to execute when altmode device of
  166. * interest probes successfully
  167. * @priv: Client's private data which is passed back when cb() is
  168. * called
  169. *
  170. * Returns: 0 upon success, negative upon errors.
  171. */
  172. int altmode_register_notifier(struct device *client_dev, void (*cb)(void *),
  173. void *priv)
  174. {
  175. int rc;
  176. struct probe_notify_node *notify_node;
  177. struct device_node *amdev_node;
  178. struct of_phandle_args pargs;
  179. struct altmode_dev *amdev;
  180. if (!client_dev || !cb || !priv)
  181. return -EINVAL;
  182. rc = of_parse_phandle_with_args(client_dev->of_node,
  183. "qcom,altmode-dev", "#altmode-cells", 0, &pargs);
  184. if (rc) {
  185. dev_err(client_dev, "Error parsing qcom,altmode-dev property: %d\n",
  186. rc);
  187. return rc;
  188. }
  189. amdev_node = pargs.np;
  190. mutex_lock(&notify_lock);
  191. amdev = to_altmode_device(amdev_node);
  192. if (amdev) {
  193. /*
  194. * If altmode device has probed already, notify
  195. * immediately.
  196. */
  197. of_node_put(amdev_node);
  198. cb(priv);
  199. } else {
  200. notify_node = kzalloc(sizeof(*notify_node), GFP_KERNEL);
  201. if (!notify_node) {
  202. mutex_unlock(&notify_lock);
  203. return -ENOMEM;
  204. }
  205. notify_node->cb = cb;
  206. notify_node->priv = priv;
  207. notify_node->amdev_node = amdev_node;
  208. list_add(&notify_node->node, &probe_notify_list);
  209. }
  210. mutex_unlock(&notify_lock);
  211. return 0;
  212. }
  213. EXPORT_SYMBOL(altmode_register_notifier);
  214. /**
  215. * altmode_deregister_notifier()
  216. * Deregister probe completion notifier.
  217. *
  218. * @client_dev: Client device pointer
  219. * @priv: Client's private data
  220. *
  221. * Returns: 0 upon success, negative upon errors.
  222. */
  223. int altmode_deregister_notifier(struct device *client_dev, void *priv)
  224. {
  225. int rc;
  226. struct device_node *amdev_node;
  227. struct probe_notify_node *pos, *tmp;
  228. struct of_phandle_args pargs;
  229. if (!client_dev)
  230. return -EINVAL;
  231. rc = of_parse_phandle_with_args(client_dev->of_node,
  232. "qcom,altmode-dev", "#altmode-cells", 0, &pargs);
  233. if (rc) {
  234. dev_err(client_dev, "Error parsing qcom,altmode-dev property: %d\n",
  235. rc);
  236. return rc;
  237. }
  238. amdev_node = pargs.np;
  239. mutex_lock(&notify_lock);
  240. list_for_each_entry_safe(pos, tmp, &probe_notify_list, node) {
  241. if (pos->amdev_node == amdev_node && pos->priv == priv) {
  242. of_node_put(pos->amdev_node);
  243. list_del(&pos->node);
  244. kfree(pos);
  245. break;
  246. }
  247. }
  248. mutex_unlock(&notify_lock);
  249. of_node_put(amdev_node);
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(altmode_deregister_notifier);
  253. /**
  254. * altmode_send_data()
  255. * Send data from altmode client to remote subsystem.
  256. *
  257. * @client: Parent altmode device of this client
  258. * @data: Data to be sent
  259. * @len: Length in bytes of the data to be sent
  260. *
  261. * Returns: 0 upon success, -EINVAL if len exceeds message buffer's
  262. * capacity, and other negative error codes as appropriate.
  263. */
  264. int altmode_send_data(struct altmode_client *client, void *data,
  265. size_t len)
  266. {
  267. struct altmode_dev *amdev = client->amdev;
  268. return __altmode_send_data(amdev, data, len);
  269. }
  270. EXPORT_SYMBOL(altmode_send_data);
  271. static void client_cb_work(struct work_struct *work)
  272. {
  273. struct altmode_client *amclient = container_of(work,
  274. struct altmode_client, client_cb_work);
  275. amclient->data.callback(amclient->data.priv, amclient->msg,
  276. sizeof(amclient->msg));
  277. }
  278. /**
  279. * altmode_register_client()
  280. * Register with altmode to receive PMIC GLINK messages from remote
  281. * subsystem.
  282. *
  283. * @client_dev: Client device pointer
  284. * @client_data: Details identifying altmode client uniquely
  285. *
  286. * Returns: Valid altmode client pointer upon success, ERR_PTRs
  287. * upon errors.
  288. *
  289. * Notes: client_data should contain a unique SVID.
  290. */
  291. struct altmode_client *altmode_register_client(struct device *client_dev,
  292. const struct altmode_client_data *client_data)
  293. {
  294. int rc, key;
  295. struct altmode_dev *amdev;
  296. struct of_phandle_args pargs;
  297. struct device_node *amdev_node;
  298. struct altmode_client *amclient;
  299. if (!client_dev || !client_data)
  300. return ERR_PTR(-EINVAL);
  301. if (!client_data->name || !client_data->priv || !client_data->callback
  302. || !client_data->svid)
  303. return ERR_PTR(-EINVAL);
  304. rc = of_parse_phandle_with_args(client_dev->of_node,
  305. "qcom,altmode-dev", "#altmode-cells", 0, &pargs);
  306. if (rc) {
  307. dev_err(client_dev, "Error parsing qcom,altmode-dev property: %d\n",
  308. rc);
  309. return ERR_PTR(rc);
  310. }
  311. if (pargs.args_count != 1) {
  312. dev_err(client_dev, "Error in port_index specification\n");
  313. return ERR_PTR(-EINVAL);
  314. }
  315. if (pargs.args[0] >= MAX_NUM_PORTS) {
  316. dev_err(client_dev, "Invalid port_index: %d, max is %d\n",
  317. pargs.args[0], MAX_NUM_PORTS - 1);
  318. return ERR_PTR(-EINVAL);
  319. }
  320. amdev_node = pargs.np;
  321. amdev = to_altmode_device(amdev_node);
  322. of_node_put(amdev_node);
  323. if (!amdev)
  324. return ERR_PTR(-EPROBE_DEFER);
  325. amclient = kzalloc(sizeof(*amclient), GFP_KERNEL);
  326. if (!amclient)
  327. return ERR_PTR(-ENOMEM);
  328. amclient->amdev = amdev;
  329. amclient->port_index = pargs.args[0];
  330. amclient->data.svid = client_data->svid;
  331. amclient->data.priv = client_data->priv;
  332. amclient->data.callback = client_data->callback;
  333. INIT_WORK(&amclient->client_cb_work, client_cb_work);
  334. amclient->data.name = kstrdup(client_data->name, GFP_KERNEL);
  335. if (!amclient->data.name) {
  336. kfree(amclient);
  337. return ERR_PTR(-ENOMEM);
  338. }
  339. mutex_lock(&amdev->client_lock);
  340. key = IDR_KEY(amclient);
  341. rc = idr_alloc(&amdev->client_idr, amclient, key, key + 1, GFP_KERNEL);
  342. if (rc < 0) {
  343. pr_err("Error in allocating idr for client %s: %d\n",
  344. client_data->name, rc);
  345. mutex_unlock(&amdev->client_lock);
  346. kfree(amclient->data.name);
  347. kfree(amclient);
  348. return ERR_PTR(rc);
  349. }
  350. list_add(&amclient->c_node, &amdev->client_list);
  351. mutex_unlock(&amdev->client_lock);
  352. if (!atomic_read(&amdev->pan_en_sent))
  353. schedule_delayed_work(&amdev->send_pan_en_work,
  354. msecs_to_jiffies(20));
  355. return amclient;
  356. }
  357. EXPORT_SYMBOL(altmode_register_client);
  358. /**
  359. * altmode_deregister_client()
  360. * Deregister altmode client to stop receiving PMIC GLINK messages
  361. * specific to its SVID from remote subsystem.
  362. *
  363. * @client: Client returned by altmode_register_client()
  364. *
  365. * Returns: 0 upon success, negative upon errors.
  366. *
  367. * Notes: This does not stop the transmission of the messages by
  368. * the remote subsystem - only the reception of them by
  369. * the client.
  370. */
  371. int altmode_deregister_client(struct altmode_client *client)
  372. {
  373. struct altmode_dev *amdev;
  374. struct altmode_client *pos, *tmp;
  375. if (!client || !client->amdev)
  376. return -ENODEV;
  377. amdev = client->amdev;
  378. cancel_work_sync(&client->client_cb_work);
  379. mutex_lock(&amdev->client_lock);
  380. idr_remove(&amdev->client_idr, IDR_KEY(client));
  381. list_for_each_entry_safe(pos, tmp, &amdev->client_list, c_node) {
  382. if (pos == client)
  383. list_del(&pos->c_node);
  384. }
  385. mutex_unlock(&amdev->client_lock);
  386. kfree(client->data.name);
  387. kfree(client);
  388. return 0;
  389. }
  390. EXPORT_SYMBOL(altmode_deregister_client);
  391. static void altmode_send_pan_en(struct work_struct *work)
  392. {
  393. int rc;
  394. u32 enable_msg = ALTMODE_PAN_EN;
  395. struct altmode_dev *amdev = container_of(work, struct altmode_dev,
  396. send_pan_en_work.work);
  397. rc = __altmode_send_data(amdev, &enable_msg, sizeof(enable_msg));
  398. if (rc < 0) {
  399. pr_err("Failed to send PAN EN: %d\n", rc);
  400. return;
  401. }
  402. atomic_set(&amdev->pan_en_sent, 1);
  403. altmode_dbg("Sent PAN EN\n");
  404. }
  405. static int altmode_send_ack(struct altmode_dev *amdev, u8 port_index)
  406. {
  407. int rc;
  408. struct altmode_pan_ack_msg ack;
  409. ack.cmd_type = ALTMODE_PAN_ACK;
  410. ack.port_index = port_index;
  411. rc = __altmode_send_data(amdev, &ack, sizeof(ack));
  412. if (rc < 0) {
  413. pr_err("port %u: Failed to send PAN ACK: %d\n", port_index, rc);
  414. return rc;
  415. }
  416. altmode_dbg("port %u: Sent PAN ACK\n", port_index);
  417. return rc;
  418. }
  419. static void altmode_state_cb(void *priv, enum pmic_glink_state state)
  420. {
  421. struct altmode_dev *amdev = priv;
  422. altmode_dbg("state: %d\n", state);
  423. switch (state) {
  424. case PMIC_GLINK_STATE_DOWN:
  425. /* As of now, nothing to do */
  426. break;
  427. case PMIC_GLINK_STATE_UP:
  428. mutex_lock(&amdev->client_lock);
  429. if (!list_empty(&amdev->client_list))
  430. schedule_delayed_work(&amdev->send_pan_en_work,
  431. msecs_to_jiffies(20));
  432. mutex_unlock(&amdev->client_lock);
  433. break;
  434. default:
  435. return;
  436. }
  437. }
  438. static void altmode_send_pan_ack(struct work_struct *work)
  439. {
  440. struct altmode_dev *amdev = container_of(work, struct altmode_dev,
  441. send_pan_ack_work.work);
  442. altmode_send_ack(amdev, amdev->ack_port_index);
  443. }
  444. #define USBC_NOTIFY_IND_MASK GENMASK(7, 0)
  445. #define GET_OP(opcode) (opcode & USBC_NOTIFY_IND_MASK)
  446. #define GET_SVID(opcode) (opcode >> 16)
  447. static int altmode_callback(void *priv, void *data, size_t len)
  448. {
  449. u16 svid, op;
  450. struct usbc_notify_ind_msg *notify_msg;
  451. struct pmic_glink_hdr *hdr = data;
  452. struct altmode_dev *amdev = priv;
  453. struct altmode_client *amclient;
  454. u8 port_index;
  455. altmode_dbg("len: %zu owner: %u type: %u opcode %04x\n", len, hdr->owner,
  456. hdr->type, hdr->opcode);
  457. /*
  458. * For DisplayPort alt mode, the hdr->opcode is designed as follows:
  459. *
  460. * hdr->opcode = (svid << 16) | USBC_NOTIFY_IND
  461. */
  462. op = GET_OP(hdr->opcode);
  463. svid = GET_SVID(hdr->opcode);
  464. switch (op) {
  465. case USBC_CMD_WRITE_REQ:
  466. complete(&amdev->response_received);
  467. break;
  468. case USBC_NOTIFY_IND:
  469. if (len != sizeof(*notify_msg)) {
  470. altmode_dbg("Expected length %u, got: %zu\n",
  471. sizeof(*notify_msg), len);
  472. return -EINVAL;
  473. }
  474. notify_msg = data;
  475. port_index = notify_msg->payload[0];
  476. mutex_lock(&amdev->client_lock);
  477. amclient = idr_find(&amdev->client_idr, IDR_KEY_GEN(svid,
  478. port_index));
  479. mutex_unlock(&amdev->client_lock);
  480. if (!amclient) {
  481. altmode_dbg("No client associated with SVID %#x port %u\n",
  482. svid, port_index);
  483. amdev->ack_port_index = port_index;
  484. schedule_delayed_work(&amdev->send_pan_ack_work,
  485. msecs_to_jiffies(20));
  486. return 0;
  487. }
  488. altmode_dbg("Payload: %*ph\n", NOTIFY_PAYLOAD_SIZE,
  489. notify_msg->payload);
  490. cancel_work_sync(&amclient->client_cb_work);
  491. memcpy(&amclient->msg, notify_msg->payload,
  492. sizeof(amclient->msg));
  493. schedule_work(&amclient->client_cb_work);
  494. break;
  495. default:
  496. break;
  497. }
  498. return 0;
  499. }
  500. static void altmode_notify_clients(struct altmode_dev *amdev)
  501. {
  502. struct altmode_dev *pos_amdev;
  503. struct probe_notify_node *pos, *tmp;
  504. mutex_lock(&notify_lock);
  505. list_for_each_entry_safe(pos, tmp, &probe_notify_list, node) {
  506. pos_amdev = to_altmode_device(pos->amdev_node);
  507. if (!pos_amdev)
  508. continue;
  509. if (pos_amdev == amdev) {
  510. pos->cb(pos->priv);
  511. of_node_put(pos->amdev_node);
  512. list_del(&pos->node);
  513. kfree(pos);
  514. }
  515. }
  516. mutex_unlock(&notify_lock);
  517. }
  518. #ifdef CONFIG_QTI_PMIC_GLINK_CLIENT_DEBUG
  519. static int pan_en_write(void *data, u64 val)
  520. {
  521. struct altmode_dev *amdev = data;
  522. schedule_delayed_work(&amdev->send_pan_en_work,
  523. msecs_to_jiffies(20));
  524. return 0;
  525. }
  526. DEFINE_DEBUGFS_ATTRIBUTE(pan_en_fops, NULL, pan_en_write, "%llu\n");
  527. static int send_ack_write(void *data, u64 val)
  528. {
  529. int rc;
  530. struct altmode_dev *amdev = data;
  531. struct altmode_pan_ack_msg ack;
  532. if (val >= MAX_NUM_PORTS)
  533. return -EINVAL;
  534. ack.cmd_type = ALTMODE_PAN_ACK;
  535. ack.port_index = val;
  536. rc = __altmode_send_data(amdev, &ack, sizeof(ack));
  537. if (rc < 0) {
  538. dev_err(amdev->dev, "port %d: Failed sending PAN ACK: %llu\n",
  539. val, rc);
  540. return rc;
  541. }
  542. dev_dbg(amdev->dev, "port %llu: Sent PAN ACK via debugfs\n", val);
  543. return 0;
  544. }
  545. DEFINE_DEBUGFS_ATTRIBUTE(send_ack_fops, NULL, send_ack_write, "%llu\n");
  546. static int altmode_setup_debugfs(struct altmode_dev *amdev)
  547. {
  548. int rc;
  549. struct dentry *am_dir, *file;
  550. am_dir = debugfs_create_dir("altmode", NULL);
  551. if (IS_ERR(am_dir)) {
  552. rc = PTR_ERR(am_dir);
  553. dev_err(amdev->dev, "Failed to create altmode directory: %d\n",
  554. rc);
  555. return rc;
  556. }
  557. file = debugfs_create_file_unsafe("send_pan_en", 0200, am_dir, amdev,
  558. &pan_en_fops);
  559. if (IS_ERR(file)) {
  560. rc = PTR_ERR(file);
  561. dev_err(amdev->dev, "Failed to create send_pan_en: %d\n", rc);
  562. goto error;
  563. }
  564. file = debugfs_create_file_unsafe("send_pan_ack", 0200, am_dir,
  565. amdev, &send_ack_fops);
  566. if (IS_ERR(file)) {
  567. rc = PTR_ERR(file);
  568. dev_err(amdev->dev, "Failed to create send_pan_ack: %d\n", rc);
  569. goto error;
  570. }
  571. amdev->debugfs_dir = am_dir;
  572. return 0;
  573. error:
  574. debugfs_remove_recursive(am_dir);
  575. return rc;
  576. }
  577. #else
  578. static int altmode_setup_debugfs(struct altmode_dev *amdev)
  579. {
  580. return 0;
  581. }
  582. #endif
  583. static int altmode_probe(struct platform_device *pdev)
  584. {
  585. int rc;
  586. struct altmode_dev *amdev;
  587. struct pmic_glink_client_data pgclient_data = { };
  588. struct device *dev = &pdev->dev;
  589. amdev = devm_kzalloc(&pdev->dev, sizeof(*amdev), GFP_KERNEL);
  590. if (!amdev)
  591. return -ENOMEM;
  592. amdev->dev = dev;
  593. mutex_init(&amdev->client_lock);
  594. idr_init(&amdev->client_idr);
  595. init_completion(&amdev->response_received);
  596. INIT_DELAYED_WORK(&amdev->send_pan_en_work, altmode_send_pan_en);
  597. INIT_DELAYED_WORK(&amdev->send_pan_ack_work, altmode_send_pan_ack);
  598. INIT_LIST_HEAD(&amdev->d_node);
  599. INIT_LIST_HEAD(&amdev->client_list);
  600. pgclient_data.id = MSG_OWNER_USBC_PAN;
  601. pgclient_data.name = "altmode";
  602. pgclient_data.msg_cb = altmode_callback;
  603. pgclient_data.priv = amdev;
  604. pgclient_data.state_cb = altmode_state_cb;
  605. amdev->pgclient = pmic_glink_register_client(amdev->dev,
  606. &pgclient_data);
  607. if (IS_ERR(amdev->pgclient)) {
  608. rc = PTR_ERR(amdev->pgclient);
  609. if (rc != -EPROBE_DEFER)
  610. dev_err(dev, "Error in pmic_glink registration: %d\n",
  611. rc);
  612. goto error_register;
  613. }
  614. platform_set_drvdata(pdev, amdev);
  615. rc = altmode_setup_debugfs(amdev);
  616. if (rc < 0) {
  617. dev_err(amdev->dev, "Failed to create debugfs: %d\n", rc);
  618. goto unreg_pmic_glink;
  619. }
  620. altmode_ipc_log = ipc_log_context_create(NUM_LOG_PAGES, "altmode", 0);
  621. if (!altmode_ipc_log)
  622. dev_warn(dev, "Error in creating ipc_log_context\n");
  623. altmode_notify_clients(amdev);
  624. return 0;
  625. unreg_pmic_glink:
  626. pmic_glink_unregister_client(amdev->pgclient);
  627. error_register:
  628. idr_destroy(&amdev->client_idr);
  629. return rc;
  630. }
  631. static int altmode_remove(struct platform_device *pdev)
  632. {
  633. int rc;
  634. struct altmode_dev *amdev = platform_get_drvdata(pdev);
  635. struct altmode_client *client, *tmp;
  636. struct probe_notify_node *npos, *ntmp;
  637. debugfs_remove_recursive(amdev->debugfs_dir);
  638. cancel_delayed_work_sync(&amdev->send_pan_en_work);
  639. cancel_delayed_work_sync(&amdev->send_pan_ack_work);
  640. idr_destroy(&amdev->client_idr);
  641. atomic_set(&amdev->pan_en_sent, 0);
  642. mutex_lock(&notify_lock);
  643. list_for_each_entry_safe(npos, ntmp, &probe_notify_list, node) {
  644. of_node_put(npos->amdev_node);
  645. list_del(&npos->node);
  646. kfree(npos);
  647. }
  648. mutex_unlock(&notify_lock);
  649. mutex_lock(&amdev->client_lock);
  650. list_for_each_entry_safe(client, tmp, &amdev->client_list, c_node)
  651. list_del(&client->c_node);
  652. mutex_unlock(&amdev->client_lock);
  653. ipc_log_context_destroy(altmode_ipc_log);
  654. altmode_ipc_log = NULL;
  655. rc = pmic_glink_unregister_client(amdev->pgclient);
  656. if (rc < 0)
  657. dev_err(amdev->dev, "Error in pmic_glink de-registration: %d\n",
  658. rc);
  659. return rc;
  660. }
  661. static const struct of_device_id altmode_match_table[] = {
  662. { .compatible = "qcom,altmode-glink" },
  663. {},
  664. };
  665. static struct platform_driver altmode_driver = {
  666. .driver = {
  667. .name = "altmode-glink",
  668. .of_match_table = altmode_match_table,
  669. },
  670. .probe = altmode_probe,
  671. .remove = altmode_remove,
  672. };
  673. module_platform_driver(altmode_driver);
  674. MODULE_DESCRIPTION("QTI Type-C Alt Mode over GLINK");
  675. MODULE_LICENSE("GPL v2");