soundwire.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/of_device.h>
  19. #include <linux/completion.h>
  20. #include <linux/idr.h>
  21. #include <linux/pm_runtime.h>
  22. #include <soc/soundwire.h>
  23. struct boardinfo {
  24. struct list_head list;
  25. struct swr_boardinfo board_info;
  26. };
  27. static LIST_HEAD(board_list);
  28. static LIST_HEAD(swr_master_list);
  29. static DEFINE_MUTEX(board_lock);
  30. static DEFINE_IDR(master_idr);
  31. static DEFINE_MUTEX(swr_lock);
  32. static struct device_type swr_dev_type;
  33. #define SOUNDWIRE_NAME_SIZE 32
  34. static void swr_master_put(struct swr_master *master)
  35. {
  36. if (master)
  37. put_device(&master->dev);
  38. }
  39. static struct swr_master *swr_master_get(struct swr_master *master)
  40. {
  41. if (!master || !get_device(&master->dev))
  42. return NULL;
  43. return master;
  44. }
  45. static void swr_dev_release(struct device *dev)
  46. {
  47. struct swr_device *swr_dev = to_swr_device(dev);
  48. struct swr_master *master;
  49. if (!swr_dev)
  50. return;
  51. master = swr_dev->master;
  52. if (!master)
  53. return;
  54. mutex_lock(&master->mlock);
  55. list_del_init(&swr_dev->dev_list);
  56. mutex_unlock(&master->mlock);
  57. swr_master_put(swr_dev->master);
  58. kfree(swr_dev);
  59. }
  60. /**
  61. * swr_remove_device - remove a soundwire device
  62. * @swr_dev: soundwire device to remove
  63. *
  64. * Remove a soundwire device. Go through the soundwire
  65. * device list that master has and remove swr_dev from
  66. * it.
  67. */
  68. void swr_remove_device(struct swr_device *swr_dev)
  69. {
  70. struct swr_device *swr_dev_loop, *safe;
  71. /*
  72. * master still has reference to all nodes and deletes
  73. * at platform_unregister, so need to init the deleted
  74. * entry
  75. */
  76. list_for_each_entry_safe(swr_dev_loop, safe,
  77. &swr_dev->master->devices,
  78. dev_list) {
  79. if (swr_dev == swr_dev_loop)
  80. list_del_init(&swr_dev_loop->dev_list);
  81. }
  82. }
  83. EXPORT_SYMBOL(swr_remove_device);
  84. /**
  85. * swr_new_device - instantiate a new soundwire device
  86. * @master: Controller to which device is connected
  87. * @info: Describes the soundwire device
  88. * Context: can sleep
  89. *
  90. * Create a soundwire device. Binding is handled through driver model
  91. * probe/remove methods. A driver may be bound to this device when
  92. * the function gets returned.
  93. *
  94. * Returns a soundwire new device or NULL
  95. */
  96. struct swr_device *swr_new_device(struct swr_master *master,
  97. struct swr_boardinfo const *info)
  98. {
  99. int result;
  100. struct swr_device *swr;
  101. if (!master || !swr_master_get(master)) {
  102. pr_err("%s: master is NULL\n", __func__);
  103. return NULL;
  104. }
  105. swr = kzalloc(sizeof(*swr), GFP_KERNEL);
  106. if (!swr) {
  107. put_device(&master->dev);
  108. return NULL;
  109. }
  110. swr->master = master;
  111. swr->addr = info->addr;
  112. strlcpy(swr->name, info->name, sizeof(swr->name));
  113. swr->dev.type = &swr_dev_type;
  114. swr->dev.parent = &master->dev;
  115. swr->dev.bus = &soundwire_type;
  116. swr->dev.release = swr_dev_release;
  117. swr->dev.of_node = info->of_node;
  118. mutex_lock(&master->mlock);
  119. list_add_tail(&swr->dev_list, &master->devices);
  120. mutex_unlock(&master->mlock);
  121. dev_set_name(&swr->dev, "%s.%lx", swr->name, swr->addr);
  122. result = device_register(&swr->dev);
  123. if (result) {
  124. dev_err(&master->dev, "device [%s] register failed err %d\n",
  125. swr->name, result);
  126. goto err_out;
  127. }
  128. dev_dbg(&master->dev, "Device [%s] registered with bus id %s\n",
  129. swr->name, dev_name(&swr->dev));
  130. return swr;
  131. err_out:
  132. dev_dbg(&master->dev, "Failed to register swr device %s at 0x%lx %d\n",
  133. swr->name, swr->addr, result);
  134. swr_master_put(master);
  135. kfree(swr);
  136. return NULL;
  137. }
  138. EXPORT_SYMBOL(swr_new_device);
  139. /**
  140. * of_register_swr_devices - register child devices on to the soundwire bus
  141. * @master: pointer to soundwire master device
  142. *
  143. * Registers a soundwire device for each child node of master node which has
  144. * a "swr-devid" property
  145. *
  146. */
  147. int of_register_swr_devices(struct swr_master *master)
  148. {
  149. struct swr_device *swr;
  150. struct device_node *node;
  151. if (!master->dev.of_node)
  152. return -EINVAL;
  153. for_each_available_child_of_node(master->dev.of_node, node) {
  154. struct swr_boardinfo info = {};
  155. u64 addr;
  156. dev_dbg(&master->dev, "of_swr:register %s\n", node->full_name);
  157. if (of_modalias_node(node, info.name, sizeof(info.name)) < 0) {
  158. dev_err(&master->dev, "of_swr:modalias failure %s\n",
  159. node->full_name);
  160. continue;
  161. }
  162. if (of_property_read_u64(node, "reg", &addr)) {
  163. dev_err(&master->dev, "of_swr:invalid reg %s\n",
  164. node->full_name);
  165. continue;
  166. }
  167. info.addr = addr;
  168. info.of_node = of_node_get(node);
  169. master->num_dev++;
  170. swr = swr_new_device(master, &info);
  171. if (!swr) {
  172. dev_err(&master->dev, "of_swr: Register failed %s\n",
  173. node->full_name);
  174. of_node_put(node);
  175. master->num_dev--;
  176. continue;
  177. }
  178. }
  179. return 0;
  180. }
  181. EXPORT_SYMBOL(of_register_swr_devices);
  182. /**
  183. * swr_port_response - response from master to free the completed transaction
  184. * @mstr: pointer to soundwire master device
  185. * @tid: transaction id that indicates transaction to be freed.
  186. *
  187. * Master calls this function to free the compeleted transaction memory
  188. */
  189. void swr_port_response(struct swr_master *mstr, u8 tid)
  190. {
  191. struct swr_params *txn;
  192. txn = mstr->port_txn[tid];
  193. if (txn == NULL) {
  194. dev_err(&mstr->dev, "%s: transaction is already NULL\n",
  195. __func__);
  196. return;
  197. }
  198. mstr->port_txn[tid] = NULL;
  199. kfree(txn);
  200. }
  201. EXPORT_SYMBOL(swr_port_response);
  202. /**
  203. * swr_remove_from_group - remove soundwire slave devices from group
  204. * @dev: pointer to the soundwire slave device
  205. * dev_num: device number of the soundwire slave device
  206. *
  207. * Returns error code for failure and 0 for success
  208. */
  209. int swr_remove_from_group(struct swr_device *dev, u8 dev_num)
  210. {
  211. struct swr_master *master;
  212. if (!dev)
  213. return -ENODEV;
  214. master = dev->master;
  215. if (!master)
  216. return -EINVAL;
  217. if (!dev->group_id)
  218. return 0;
  219. if (master->gr_sid == dev_num)
  220. return 0;
  221. if (master->remove_from_group && master->remove_from_group(master))
  222. dev_dbg(&master->dev, "%s: falling back to GROUP_NONE\n",
  223. __func__);
  224. return 0;
  225. }
  226. EXPORT_SYMBOL(swr_remove_from_group);
  227. /**
  228. * swr_slvdev_datapath_control - Enables/Disables soundwire slave device
  229. * data path
  230. * @dev: pointer to soundwire slave device
  231. * @dev_num: device number of the soundwire slave device
  232. *
  233. * Returns error code for failure and 0 for success
  234. */
  235. int swr_slvdev_datapath_control(struct swr_device *dev, u8 dev_num,
  236. bool enable)
  237. {
  238. struct swr_master *master;
  239. int ret = 0;
  240. if (!dev)
  241. return -ENODEV;
  242. master = dev->master;
  243. if (!master)
  244. return -EINVAL;
  245. if (dev->group_id) {
  246. /* Broadcast */
  247. if (master->gr_sid != dev_num) {
  248. if (!master->gr_sid)
  249. master->gr_sid = dev_num;
  250. else
  251. return 0;
  252. }
  253. }
  254. if (master->slvdev_datapath_control)
  255. ret = master->slvdev_datapath_control(master, enable);
  256. return ret;
  257. }
  258. EXPORT_SYMBOL(swr_slvdev_datapath_control);
  259. /**
  260. * swr_connect_port - enable soundwire slave port(s)
  261. * @dev: pointer to soundwire slave device
  262. * @port_id: logical port id(s) of soundwire slave device
  263. * @num_port: number of slave device ports need to be enabled
  264. * @ch_mask: channels for each port that needs to be enabled
  265. * @ch_rate: rate at which each port/channels operate
  266. * @num_ch: number of channels for each port
  267. *
  268. * soundwire slave device call swr_connect_port API to enable all/some of
  269. * its ports and corresponding channels and channel rate. This API will
  270. * call master connect_port callback function to calculate frame structure
  271. * and enable master and slave ports
  272. */
  273. int swr_connect_port(struct swr_device *dev, u8 *port_id, u8 num_port,
  274. u8 *ch_mask, u32 *ch_rate, u8 *num_ch, u8 *port_type)
  275. {
  276. u8 i = 0;
  277. int ret = 0;
  278. struct swr_params *txn = NULL;
  279. struct swr_params **temp_txn = NULL;
  280. struct swr_master *master = dev->master;
  281. if (!master) {
  282. pr_err("%s: Master is NULL\n", __func__);
  283. return -EINVAL;
  284. }
  285. if (num_port > SWR_MAX_DEV_PORT_NUM) {
  286. dev_err(&master->dev, "%s: num_port %d exceeds max port %d\n",
  287. __func__, num_port, SWR_MAX_DEV_PORT_NUM);
  288. return -EINVAL;
  289. }
  290. /*
  291. * create "txn" to accommodate ports enablement of
  292. * different slave devices calling swr_connect_port at the
  293. * same time. Once master process the txn data, it calls
  294. * swr_port_response() to free the transaction. Maximum
  295. * of 256 transactions can be allocated.
  296. */
  297. txn = kzalloc(sizeof(struct swr_params), GFP_KERNEL);
  298. if (!txn)
  299. return -ENOMEM;
  300. mutex_lock(&master->mlock);
  301. for (i = 0; i < master->last_tid; i++) {
  302. if (master->port_txn[i] == NULL)
  303. break;
  304. }
  305. if (i >= master->last_tid) {
  306. if (master->last_tid == 255) {
  307. mutex_unlock(&master->mlock);
  308. kfree(txn);
  309. dev_err(&master->dev, "%s Max tid reached\n",
  310. __func__);
  311. return -ENOMEM;
  312. }
  313. temp_txn = krealloc(master->port_txn,
  314. (i + 1) * sizeof(struct swr_params *),
  315. GFP_KERNEL);
  316. if (!temp_txn) {
  317. mutex_unlock(&master->mlock);
  318. kfree(txn);
  319. dev_err(&master->dev, "%s Not able to allocate\n"
  320. "master port transaction memory\n",
  321. __func__);
  322. return -ENOMEM;
  323. }
  324. master->port_txn = temp_txn;
  325. master->last_tid++;
  326. }
  327. master->port_txn[i] = txn;
  328. mutex_unlock(&master->mlock);
  329. txn->tid = i;
  330. txn->dev_num = dev->dev_num;
  331. txn->num_port = num_port;
  332. for (i = 0; i < num_port; i++) {
  333. txn->port_id[i] = port_id[i];
  334. txn->num_ch[i] = num_ch[i];
  335. txn->ch_rate[i] = ch_rate[i];
  336. txn->ch_en[i] = ch_mask[i];
  337. txn->port_type[i] = port_type[i];
  338. }
  339. ret = master->connect_port(master, txn);
  340. return ret;
  341. }
  342. EXPORT_SYMBOL(swr_connect_port);
  343. /**
  344. * swr_disconnect_port - disable soundwire slave port(s)
  345. * @dev: pointer to soundwire slave device
  346. * @port_id: logical port id(s) of soundwire slave device
  347. * @num_port: number of slave device ports need to be disabled
  348. *
  349. * soundwire slave device call swr_disconnect_port API to disable all/some of
  350. * its ports. This API will call master disconnect_port callback function to
  351. * disable master and slave port and (re)configure frame structure
  352. */
  353. int swr_disconnect_port(struct swr_device *dev, u8 *port_id, u8 num_port,
  354. u8 *ch_mask, u8 *port_type)
  355. {
  356. u8 i = 0;
  357. int ret;
  358. struct swr_params *txn = NULL;
  359. struct swr_params **temp_txn = NULL;
  360. struct swr_master *master = dev->master;
  361. if (!master) {
  362. pr_err("%s: Master is NULL\n", __func__);
  363. return -EINVAL;
  364. }
  365. if (num_port > SWR_MAX_DEV_PORT_NUM) {
  366. dev_err(&master->dev, "%s: num_port %d exceeds max port %d\n",
  367. __func__, num_port, SWR_MAX_DEV_PORT_NUM);
  368. return -EINVAL;
  369. }
  370. txn = kzalloc(sizeof(struct swr_params), GFP_KERNEL);
  371. if (!txn)
  372. return -ENOMEM;
  373. mutex_lock(&master->mlock);
  374. for (i = 0; i < master->last_tid; i++) {
  375. if (master->port_txn[i] == NULL)
  376. break;
  377. }
  378. if (i >= master->last_tid) {
  379. if (master->last_tid == 255) {
  380. mutex_unlock(&master->mlock);
  381. kfree(txn);
  382. dev_err(&master->dev, "%s Max tid reached\n",
  383. __func__);
  384. return -ENOMEM;
  385. }
  386. temp_txn = krealloc(master->port_txn,
  387. (i + 1) * sizeof(struct swr_params *),
  388. GFP_KERNEL);
  389. if (!temp_txn) {
  390. mutex_unlock(&master->mlock);
  391. kfree(txn);
  392. dev_err(&master->dev, "%s Not able to allocate\n"
  393. "master port transaction memory\n",
  394. __func__);
  395. return -ENOMEM;
  396. }
  397. master->port_txn = temp_txn;
  398. master->last_tid++;
  399. }
  400. master->port_txn[i] = txn;
  401. mutex_unlock(&master->mlock);
  402. txn->tid = i;
  403. txn->dev_num = dev->dev_num;
  404. txn->num_port = num_port;
  405. for (i = 0; i < num_port; i++) {
  406. txn->port_id[i] = port_id[i];
  407. txn->ch_en[i] = ch_mask[i];
  408. txn->port_type[i] = port_type[i];
  409. }
  410. ret = master->disconnect_port(master, txn);
  411. return ret;
  412. }
  413. EXPORT_SYMBOL(swr_disconnect_port);
  414. /**
  415. * swr_get_logical_dev_num - Get soundwire slave logical device number
  416. * @dev: pointer to soundwire slave device
  417. * @dev_id: physical device id of soundwire slave device
  418. * @dev_num: pointer to logical device num of soundwire slave device
  419. *
  420. * This API will get the logical device number of soundwire slave device
  421. */
  422. int swr_get_logical_dev_num(struct swr_device *dev, u64 dev_id,
  423. u8 *dev_num)
  424. {
  425. int ret = 0;
  426. struct swr_master *master = dev->master;
  427. if (!master) {
  428. pr_err("%s: Master is NULL\n", __func__);
  429. return -EINVAL;
  430. }
  431. mutex_lock(&master->mlock);
  432. ret = master->get_logical_dev_num(master, dev_id, dev_num);
  433. if (ret) {
  434. pr_err("%s: Error %d to get logical addr for device %llx\n",
  435. __func__, ret, dev_id);
  436. }
  437. mutex_unlock(&master->mlock);
  438. return ret;
  439. }
  440. EXPORT_SYMBOL(swr_get_logical_dev_num);
  441. /**
  442. * swr_read - read soundwire slave device registers
  443. * @dev: pointer to soundwire slave device
  444. * @dev_num: logical device num of soundwire slave device
  445. * @reg_addr: base register address that needs to be read
  446. * @buf: pointer to store the values of registers from base address
  447. * @len: length of the buffer
  448. *
  449. * This API will read the value of the register address from
  450. * soundwire slave device
  451. */
  452. int swr_read(struct swr_device *dev, u8 dev_num, u16 reg_addr,
  453. void *buf, u32 len)
  454. {
  455. struct swr_master *master = dev->master;
  456. if (!master)
  457. return -EINVAL;
  458. return master->read(master, dev_num, reg_addr, buf, len);
  459. }
  460. EXPORT_SYMBOL(swr_read);
  461. /**
  462. * swr_bulk_write - write soundwire slave device registers
  463. * @dev: pointer to soundwire slave device
  464. * @dev_num: logical device num of soundwire slave device
  465. * @reg_addr: register address of soundwire slave device
  466. * @buf: contains value of register address
  467. * @len: indicates number of registers
  468. *
  469. * This API will write the value of the register address to
  470. * soundwire slave device
  471. */
  472. int swr_bulk_write(struct swr_device *dev, u8 dev_num, void *reg,
  473. const void *buf, size_t len)
  474. {
  475. struct swr_master *master;
  476. if (!dev || !dev->master)
  477. return -EINVAL;
  478. master = dev->master;
  479. if (dev->group_id) {
  480. if (master->gr_sid != dev_num) {
  481. if (!master->gr_sid)
  482. master->gr_sid = dev_num;
  483. else
  484. return 0;
  485. }
  486. dev_num = dev->group_id;
  487. }
  488. if (master->bulk_write)
  489. return master->bulk_write(master, dev_num, reg, buf, len);
  490. return -EOPNOTSUPP;
  491. }
  492. EXPORT_SYMBOL(swr_bulk_write);
  493. /**
  494. * swr_write - write soundwire slave device registers
  495. * @dev: pointer to soundwire slave device
  496. * @dev_num: logical device num of soundwire slave device
  497. * @reg_addr: register address of soundwire slave device
  498. * @buf: contains value of register address
  499. *
  500. * This API will write the value of the register address to
  501. * soundwire slave device
  502. */
  503. int swr_write(struct swr_device *dev, u8 dev_num, u16 reg_addr,
  504. const void *buf)
  505. {
  506. struct swr_master *master = dev->master;
  507. if (!master)
  508. return -EINVAL;
  509. if (dev->group_id) {
  510. if (master->gr_sid != dev_num) {
  511. if (!master->gr_sid)
  512. master->gr_sid = dev_num;
  513. else
  514. return 0;
  515. }
  516. dev_num = dev->group_id;
  517. }
  518. return master->write(master, dev_num, reg_addr, buf);
  519. }
  520. EXPORT_SYMBOL(swr_write);
  521. /**
  522. * swr_device_up - Function to bringup the soundwire slave device
  523. * @swr_dev: pointer to soundwire slave device
  524. * Context: can sleep
  525. *
  526. * This API will be called by soundwire master to bringup the slave
  527. * device.
  528. */
  529. int swr_device_up(struct swr_device *swr_dev)
  530. {
  531. struct device *dev;
  532. const struct swr_driver *sdrv;
  533. if (!swr_dev)
  534. return -EINVAL;
  535. dev = &swr_dev->dev;
  536. sdrv = to_swr_driver(dev->driver);
  537. if (!sdrv)
  538. return 0;
  539. if (sdrv->device_up)
  540. return sdrv->device_up(to_swr_device(dev));
  541. return -ENODEV;
  542. }
  543. EXPORT_SYMBOL(swr_device_up);
  544. /**
  545. * swr_device_down - Function to call soundwire slave device down
  546. * @swr_dev: pointer to soundwire slave device
  547. * Context: can sleep
  548. *
  549. * This API will be called by soundwire master to put slave device in
  550. * shutdown state.
  551. */
  552. int swr_device_down(struct swr_device *swr_dev)
  553. {
  554. struct device *dev;
  555. const struct swr_driver *sdrv;
  556. if (!swr_dev)
  557. return -EINVAL;
  558. dev = &swr_dev->dev;
  559. sdrv = to_swr_driver(dev->driver);
  560. if (!sdrv)
  561. return 0;
  562. if (sdrv->device_down)
  563. return sdrv->device_down(to_swr_device(dev));
  564. return -ENODEV;
  565. }
  566. EXPORT_SYMBOL(swr_device_down);
  567. /**
  568. * swr_reset_device - reset soundwire slave device
  569. * @swr_dev: pointer to soundwire slave device
  570. * Context: can sleep
  571. *
  572. * This API will be called by soundwire master to reset the slave
  573. * device when the slave device is not responding or in undefined
  574. * state
  575. */
  576. int swr_reset_device(struct swr_device *swr_dev)
  577. {
  578. struct device *dev;
  579. const struct swr_driver *sdrv;
  580. if (!swr_dev)
  581. return -EINVAL;
  582. dev = &swr_dev->dev;
  583. sdrv = to_swr_driver(dev->driver);
  584. if (!sdrv)
  585. return -EINVAL;
  586. if (sdrv->reset_device)
  587. return sdrv->reset_device(to_swr_device(dev));
  588. return -ENODEV;
  589. }
  590. EXPORT_SYMBOL(swr_reset_device);
  591. /**
  592. * swr_set_device_group - Assign group id to the slave devices
  593. * @swr_dev: pointer to soundwire slave device
  594. * @id: group id to be assigned to slave device
  595. * Context: can sleep
  596. *
  597. * This API will be called either from soundwire master or slave
  598. * device to assign group id.
  599. */
  600. int swr_set_device_group(struct swr_device *swr_dev, u8 id)
  601. {
  602. struct swr_master *master;
  603. if (!swr_dev)
  604. return -EINVAL;
  605. swr_dev->group_id = id;
  606. master = swr_dev->master;
  607. if (!id && master)
  608. master->gr_sid = 0;
  609. return 0;
  610. }
  611. EXPORT_SYMBOL(swr_set_device_group);
  612. static int swr_drv_probe(struct device *dev)
  613. {
  614. const struct swr_driver *sdrv = to_swr_driver(dev->driver);
  615. if (!sdrv)
  616. return -EINVAL;
  617. if (sdrv->probe)
  618. return sdrv->probe(to_swr_device(dev));
  619. return -ENODEV;
  620. }
  621. static int swr_drv_remove(struct device *dev)
  622. {
  623. const struct swr_driver *sdrv = to_swr_driver(dev->driver);
  624. if (!sdrv)
  625. return -EINVAL;
  626. if (sdrv->remove)
  627. return sdrv->remove(to_swr_device(dev));
  628. return -ENODEV;
  629. }
  630. static void swr_drv_shutdown(struct device *dev)
  631. {
  632. const struct swr_driver *sdrv = to_swr_driver(dev->driver);
  633. if (!sdrv)
  634. return;
  635. if (sdrv->shutdown)
  636. sdrv->shutdown(to_swr_device(dev));
  637. }
  638. /**
  639. * swr_driver_register - register a soundwire driver
  640. * @drv: the driver to register
  641. * Context: can sleep
  642. */
  643. int swr_driver_register(struct swr_driver *drv)
  644. {
  645. drv->driver.bus = &soundwire_type;
  646. if (drv->probe)
  647. drv->driver.probe = swr_drv_probe;
  648. if (drv->remove)
  649. drv->driver.remove = swr_drv_remove;
  650. if (drv->shutdown)
  651. drv->driver.shutdown = swr_drv_shutdown;
  652. return driver_register(&drv->driver);
  653. }
  654. EXPORT_SYMBOL(swr_driver_register);
  655. /**
  656. * swr_driver_unregister - unregister a soundwire driver
  657. * @drv: the driver to unregister
  658. */
  659. void swr_driver_unregister(struct swr_driver *drv)
  660. {
  661. if (drv)
  662. driver_unregister(&drv->driver);
  663. }
  664. EXPORT_SYMBOL(swr_driver_unregister);
  665. static void swr_match_ctrl_to_boardinfo(struct swr_master *master,
  666. struct swr_boardinfo *bi)
  667. {
  668. struct swr_device *swr;
  669. if (master->bus_num != bi->bus_num) {
  670. dev_dbg(&master->dev,
  671. "%s: master# %d and bi# %d does not match\n",
  672. __func__, master->bus_num, bi->bus_num);
  673. return;
  674. }
  675. swr = swr_new_device(master, bi);
  676. if (!swr)
  677. dev_err(&master->dev, "can't create new device for %s\n",
  678. bi->swr_slave->name);
  679. }
  680. /**
  681. * swr_master_add_boarddevices - Add devices registered by board info
  682. * @master: master to which these devices are to be added to.
  683. *
  684. * This API is called by master when it is up and running. If devices
  685. * on a master were registered before master, this will make sure that
  686. * they get probed when master is up.
  687. */
  688. void swr_master_add_boarddevices(struct swr_master *master)
  689. {
  690. struct boardinfo *bi;
  691. mutex_lock(&board_lock);
  692. list_add_tail(&master->list, &swr_master_list);
  693. list_for_each_entry(bi, &board_list, list)
  694. swr_match_ctrl_to_boardinfo(master, &bi->board_info);
  695. mutex_unlock(&board_lock);
  696. }
  697. EXPORT_SYMBOL(swr_master_add_boarddevices);
  698. struct swr_device *get_matching_swr_slave_device(struct device_node *np)
  699. {
  700. struct swr_device *swr = NULL;
  701. struct swr_master *master;
  702. mutex_lock(&board_lock);
  703. list_for_each_entry(master, &swr_master_list, list) {
  704. mutex_lock(&master->mlock);
  705. list_for_each_entry(swr, &master->devices, dev_list) {
  706. if (swr->dev.of_node == np)
  707. break;
  708. }
  709. mutex_unlock(&master->mlock);
  710. }
  711. mutex_unlock(&board_lock);
  712. return swr;
  713. }
  714. EXPORT_SYMBOL(get_matching_swr_slave_device);
  715. static void swr_unregister_device(struct swr_device *swr)
  716. {
  717. if (swr)
  718. device_unregister(&swr->dev);
  719. }
  720. static void swr_master_release(struct device *dev)
  721. {
  722. /* kfree of master done at swrm_remove of device */
  723. }
  724. #define swr_master_attr_gr NULL
  725. static struct device_type swr_master_type = {
  726. .groups = swr_master_attr_gr,
  727. .release = swr_master_release,
  728. };
  729. static int __unregister(struct device *dev, void *null)
  730. {
  731. swr_unregister_device(to_swr_device(dev));
  732. return 0;
  733. }
  734. /**
  735. * swr_unregister_master - unregister soundwire master controller
  736. * @master: the master being unregistered
  737. *
  738. * This API is called by master controller driver to unregister
  739. * master controller that was registered by swr_register_master API.
  740. */
  741. void swr_unregister_master(struct swr_master *master)
  742. {
  743. int dummy;
  744. struct swr_master *m_ctrl;
  745. mutex_lock(&swr_lock);
  746. m_ctrl = idr_find(&master_idr, master->bus_num);
  747. mutex_unlock(&swr_lock);
  748. if (m_ctrl != master)
  749. return;
  750. mutex_lock(&board_lock);
  751. list_del(&master->list);
  752. mutex_unlock(&board_lock);
  753. /* free bus id */
  754. mutex_lock(&swr_lock);
  755. idr_remove(&master_idr, master->bus_num);
  756. mutex_unlock(&swr_lock);
  757. dummy = device_for_each_child(&master->dev, NULL, __unregister);
  758. device_unregister(&master->dev);
  759. }
  760. EXPORT_SYMBOL(swr_unregister_master);
  761. /**
  762. * swr_register_master - register soundwire master controller
  763. * @master: master to be registered
  764. *
  765. * This API will register master with the framework. master->bus_num
  766. * is the desired number with which soundwire framework registers the
  767. * master.
  768. */
  769. int swr_register_master(struct swr_master *master)
  770. {
  771. int id;
  772. int status = 0;
  773. mutex_lock(&swr_lock);
  774. id = of_alias_get_id(master->dev.of_node, "swr");
  775. if (id >= 0)
  776. master->bus_num = id;
  777. id = idr_alloc(&master_idr, master, master->bus_num,
  778. master->bus_num + 1, GFP_KERNEL);
  779. mutex_unlock(&swr_lock);
  780. if (id < 0)
  781. return id;
  782. master->bus_num = id;
  783. /* Can't register until driver model init */
  784. if (WARN_ON(!soundwire_type.p)) {
  785. status = -EAGAIN;
  786. goto done;
  787. }
  788. dev_set_name(&master->dev, "swr%u", master->bus_num);
  789. master->dev.bus = &soundwire_type;
  790. master->dev.type = &swr_master_type;
  791. mutex_init(&master->mlock);
  792. status = device_register(&master->dev);
  793. if (status < 0)
  794. goto done;
  795. INIT_LIST_HEAD(&master->devices);
  796. pr_debug("%s: SWR master registered successfully %s\n",
  797. __func__, dev_name(&master->dev));
  798. return 0;
  799. done:
  800. idr_remove(&master_idr, master->bus_num);
  801. return status;
  802. }
  803. EXPORT_SYMBOL(swr_register_master);
  804. #define swr_device_attr_gr NULL
  805. #define swr_device_uevent NULL
  806. static struct device_type swr_dev_type = {
  807. .groups = swr_device_attr_gr,
  808. .uevent = swr_device_uevent,
  809. .release = swr_dev_release,
  810. };
  811. static const struct swr_device_id *swr_match(const struct swr_device_id *id,
  812. const struct swr_device *swr_dev)
  813. {
  814. while (id->name[0]) {
  815. if (strcmp(swr_dev->name, id->name) == 0)
  816. return id;
  817. id++;
  818. }
  819. return NULL;
  820. }
  821. static int swr_device_match(struct device *dev, struct device_driver *driver)
  822. {
  823. struct swr_device *swr_dev;
  824. struct swr_driver *drv = to_swr_driver(driver);
  825. if (!drv)
  826. return -EINVAL;
  827. if (dev->type == &swr_dev_type)
  828. swr_dev = to_swr_device(dev);
  829. else
  830. return 0;
  831. if (drv->id_table)
  832. return swr_match(drv->id_table, swr_dev) != NULL;
  833. if (driver->name)
  834. return strcmp(swr_dev->name, driver->name) == 0;
  835. return 0;
  836. }
  837. #ifdef CONFIG_PM_SLEEP
  838. static int swr_legacy_suspend(struct device *dev, pm_message_t mesg)
  839. {
  840. struct swr_device *swr_dev = NULL;
  841. struct swr_driver *driver;
  842. if (dev->type == &swr_dev_type)
  843. swr_dev = to_swr_device(dev);
  844. if (!swr_dev || !dev->driver)
  845. return 0;
  846. driver = to_swr_driver(dev->driver);
  847. if (!driver->suspend)
  848. return 0;
  849. return driver->suspend(swr_dev, mesg);
  850. }
  851. static int swr_legacy_resume(struct device *dev)
  852. {
  853. struct swr_device *swr_dev = NULL;
  854. struct swr_driver *driver;
  855. if (dev->type == &swr_dev_type)
  856. swr_dev = to_swr_device(dev);
  857. if (!swr_dev || !dev->driver)
  858. return 0;
  859. driver = to_swr_driver(dev->driver);
  860. if (!driver->resume)
  861. return 0;
  862. return driver->resume(swr_dev);
  863. }
  864. static int swr_pm_suspend(struct device *dev)
  865. {
  866. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  867. if (pm)
  868. return pm_generic_suspend(dev);
  869. else
  870. return swr_legacy_suspend(dev, PMSG_SUSPEND);
  871. }
  872. static int swr_pm_resume(struct device *dev)
  873. {
  874. const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
  875. if (pm)
  876. return pm_generic_resume(dev);
  877. else
  878. return swr_legacy_resume(dev);
  879. }
  880. #else
  881. #define swr_pm_suspend NULL
  882. #define swr_pm_resume NULL
  883. #endif /*CONFIG_PM_SLEEP*/
  884. static const struct dev_pm_ops soundwire_pm = {
  885. .suspend = swr_pm_suspend,
  886. .resume = swr_pm_resume,
  887. SET_RUNTIME_PM_OPS(
  888. pm_generic_suspend,
  889. pm_generic_resume,
  890. NULL
  891. )
  892. };
  893. struct device soundwire_dev = {
  894. .init_name = "soundwire",
  895. };
  896. struct bus_type soundwire_type = {
  897. .name = "soundwire",
  898. .match = swr_device_match,
  899. .pm = &soundwire_pm,
  900. };
  901. EXPORT_SYMBOL(soundwire_type);
  902. static void __exit soundwire_exit(void)
  903. {
  904. device_unregister(&soundwire_dev);
  905. bus_unregister(&soundwire_type);
  906. }
  907. static int __init soundwire_init(void)
  908. {
  909. int retval;
  910. retval = bus_register(&soundwire_type);
  911. if (!retval)
  912. retval = device_register(&soundwire_dev);
  913. if (retval)
  914. bus_unregister(&soundwire_type);
  915. return retval;
  916. }
  917. module_init(soundwire_init);
  918. module_exit(soundwire_exit);
  919. MODULE_LICENSE("GPL v2");
  920. MODULE_DESCRIPTION("Soundwire module");
  921. MODULE_ALIAS("platform:soundwire");