soundwire.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/errno.h>
  8. #include <linux/slab.h>
  9. #include <linux/init.h>
  10. #include <linux/types.h>
  11. #include <linux/of_device.h>
  12. #include <linux/completion.h>
  13. #include <linux/idr.h>
  14. #include <linux/pm_runtime.h>
  15. #include <soc/soundwire.h>
  16. struct boardinfo {
  17. struct list_head list;
  18. struct swr_boardinfo board_info;
  19. };
  20. static LIST_HEAD(board_list);
  21. static LIST_HEAD(swr_master_list);
  22. static DEFINE_MUTEX(board_lock);
  23. static DEFINE_IDR(master_idr);
  24. static DEFINE_MUTEX(swr_lock);
  25. static struct device_type swr_dev_type;
  26. #define SOUNDWIRE_NAME_SIZE 32
  27. static void swr_master_put(struct swr_master *master)
  28. {
  29. if (master)
  30. put_device(&master->dev);
  31. }
  32. static struct swr_master *swr_master_get(struct swr_master *master)
  33. {
  34. if (!master || !get_device(&master->dev))
  35. return NULL;
  36. return master;
  37. }
  38. static void swr_dev_release(struct device *dev)
  39. {
  40. struct swr_device *swr_dev = to_swr_device(dev);
  41. struct swr_master *master;
  42. if (!swr_dev)
  43. return;
  44. master = swr_dev->master;
  45. if (!master)
  46. return;
  47. mutex_lock(&master->mlock);
  48. list_del_init(&swr_dev->dev_list);
  49. mutex_unlock(&master->mlock);
  50. swr_master_put(swr_dev->master);
  51. kfree(swr_dev);
  52. }
  53. /**
  54. * swr_remove_device - remove a soundwire device
  55. * @swr_dev: soundwire device to remove
  56. *
  57. * Remove a soundwire device. Go through the soundwire
  58. * device list that master has and remove swr_dev from
  59. * it.
  60. */
  61. void swr_remove_device(struct swr_device *swr_dev)
  62. {
  63. struct swr_device *swr_dev_loop, *safe;
  64. /*
  65. * master still has reference to all nodes and deletes
  66. * at platform_unregister, so need to init the deleted
  67. * entry
  68. */
  69. list_for_each_entry_safe(swr_dev_loop, safe,
  70. &swr_dev->master->devices,
  71. dev_list) {
  72. if (swr_dev == swr_dev_loop)
  73. list_del_init(&swr_dev_loop->dev_list);
  74. }
  75. }
  76. EXPORT_SYMBOL(swr_remove_device);
  77. /**
  78. * swr_new_device - instantiate a new soundwire device
  79. * @master: Controller to which device is connected
  80. * @info: Describes the soundwire device
  81. * Context: can sleep
  82. *
  83. * Create a soundwire device. Binding is handled through driver model
  84. * probe/remove methods. A driver may be bound to this device when
  85. * the function gets returned.
  86. *
  87. * Returns a soundwire new device or NULL
  88. */
  89. struct swr_device *swr_new_device(struct swr_master *master,
  90. struct swr_boardinfo const *info)
  91. {
  92. int result;
  93. struct swr_device *swr;
  94. if (!master || !swr_master_get(master)) {
  95. pr_err("%s: master is NULL\n", __func__);
  96. return NULL;
  97. }
  98. swr = kzalloc(sizeof(*swr), GFP_KERNEL);
  99. if (!swr) {
  100. put_device(&master->dev);
  101. return NULL;
  102. }
  103. swr->master = master;
  104. swr->addr = info->addr;
  105. strlcpy(swr->name, info->name, sizeof(swr->name));
  106. swr->dev.type = &swr_dev_type;
  107. swr->dev.parent = &master->dev;
  108. swr->dev.bus = &soundwire_type;
  109. swr->dev.release = swr_dev_release;
  110. swr->dev.of_node = info->of_node;
  111. mutex_lock(&master->mlock);
  112. list_add_tail(&swr->dev_list, &master->devices);
  113. mutex_unlock(&master->mlock);
  114. dev_set_name(&swr->dev, "%s.%lx", swr->name, swr->addr);
  115. result = device_register(&swr->dev);
  116. if (result) {
  117. dev_err(&master->dev, "device [%s] register failed err %d\n",
  118. swr->name, result);
  119. goto err_out;
  120. }
  121. dev_dbg(&master->dev, "Device [%s] registered with bus id %s\n",
  122. swr->name, dev_name(&swr->dev));
  123. return swr;
  124. err_out:
  125. dev_dbg(&master->dev, "Failed to register swr device %s at 0x%lx %d\n",
  126. swr->name, swr->addr, result);
  127. swr_master_put(master);
  128. kfree(swr);
  129. return NULL;
  130. }
  131. EXPORT_SYMBOL(swr_new_device);
  132. /**
  133. * of_register_swr_devices - register child devices on to the soundwire bus
  134. * @master: pointer to soundwire master device
  135. *
  136. * Registers a soundwire device for each child node of master node which has
  137. * a "swr-devid" property
  138. *
  139. */
  140. int of_register_swr_devices(struct swr_master *master)
  141. {
  142. struct swr_device *swr;
  143. struct device_node *node;
  144. if (!master->dev.of_node)
  145. return -EINVAL;
  146. for_each_available_child_of_node(master->dev.of_node, node) {
  147. struct swr_boardinfo info = {};
  148. u64 addr;
  149. dev_dbg(&master->dev, "of_swr:register %s\n", node->full_name);
  150. if (of_modalias_node(node, info.name, sizeof(info.name)) < 0) {
  151. dev_err(&master->dev, "of_swr:modalias failure %s\n",
  152. node->full_name);
  153. continue;
  154. }
  155. if (of_property_read_u64(node, "reg", &addr)) {
  156. dev_err(&master->dev, "of_swr:invalid reg %s\n",
  157. node->full_name);
  158. continue;
  159. }
  160. info.addr = addr;
  161. info.of_node = of_node_get(node);
  162. master->num_dev++;
  163. swr = swr_new_device(master, &info);
  164. if (!swr) {
  165. dev_err(&master->dev, "of_swr: Register failed %s\n",
  166. node->full_name);
  167. of_node_put(node);
  168. master->num_dev--;
  169. continue;
  170. }
  171. }
  172. return 0;
  173. }
  174. EXPORT_SYMBOL(of_register_swr_devices);
  175. /**
  176. * swr_port_response - response from master to free the completed transaction
  177. * @mstr: pointer to soundwire master device
  178. * @tid: transaction id that indicates transaction to be freed.
  179. *
  180. * Master calls this function to free the compeleted transaction memory
  181. */
  182. void swr_port_response(struct swr_master *mstr, u8 tid)
  183. {
  184. struct swr_params *txn;
  185. txn = mstr->port_txn[tid];
  186. if (txn == NULL) {
  187. dev_err(&mstr->dev, "%s: transaction is already NULL\n",
  188. __func__);
  189. return;
  190. }
  191. mstr->port_txn[tid] = NULL;
  192. kfree(txn);
  193. }
  194. EXPORT_SYMBOL(swr_port_response);
  195. /**
  196. * swr_remove_from_group - remove soundwire slave devices from group
  197. * @dev: pointer to the soundwire slave device
  198. * dev_num: device number of the soundwire slave device
  199. *
  200. * Returns error code for failure and 0 for success
  201. */
  202. int swr_remove_from_group(struct swr_device *dev, u8 dev_num)
  203. {
  204. struct swr_master *master;
  205. if (!dev)
  206. return -ENODEV;
  207. master = dev->master;
  208. if (!master)
  209. return -EINVAL;
  210. if (!dev->group_id)
  211. return 0;
  212. if (master->gr_sid != dev_num)
  213. return 0;
  214. if (master->remove_from_group && master->remove_from_group(master))
  215. dev_dbg(&master->dev, "%s: falling back to GROUP_NONE\n",
  216. __func__);
  217. return 0;
  218. }
  219. EXPORT_SYMBOL(swr_remove_from_group);
  220. /**
  221. * swr_slvdev_datapath_control - Enables/Disables soundwire slave device
  222. * data path
  223. * @dev: pointer to soundwire slave device
  224. * @dev_num: device number of the soundwire slave device
  225. *
  226. * Returns error code for failure and 0 for success
  227. */
  228. int swr_slvdev_datapath_control(struct swr_device *dev, u8 dev_num,
  229. bool enable)
  230. {
  231. struct swr_master *master;
  232. int ret = 0;
  233. if (!dev)
  234. return -ENODEV;
  235. master = dev->master;
  236. if (!master)
  237. return -EINVAL;
  238. if (dev->group_id) {
  239. /* Broadcast */
  240. if (master->gr_sid != dev_num) {
  241. if (!master->gr_sid)
  242. master->gr_sid = dev_num;
  243. else
  244. return 0;
  245. }
  246. }
  247. if (master->slvdev_datapath_control)
  248. ret = master->slvdev_datapath_control(master, enable);
  249. return ret;
  250. }
  251. EXPORT_SYMBOL(swr_slvdev_datapath_control);
  252. /**
  253. * swr_connect_port - enable soundwire slave port(s)
  254. * @dev: pointer to soundwire slave device
  255. * @port_id: logical port id(s) of soundwire slave device
  256. * @num_port: number of slave device ports need to be enabled
  257. * @ch_mask: channels for each port that needs to be enabled
  258. * @ch_rate: rate at which each port/channels operate
  259. * @num_ch: number of channels for each port
  260. *
  261. * soundwire slave device call swr_connect_port API to enable all/some of
  262. * its ports and corresponding channels and channel rate. This API will
  263. * call master connect_port callback function to calculate frame structure
  264. * and enable master and slave ports
  265. */
  266. int swr_connect_port(struct swr_device *dev, u8 *port_id, u8 num_port,
  267. u8 *ch_mask, u32 *ch_rate, u8 *num_ch, u8 *port_type)
  268. {
  269. u8 i = 0;
  270. int ret = 0;
  271. struct swr_params *txn = NULL;
  272. struct swr_params **temp_txn = NULL;
  273. struct swr_master *master = dev->master;
  274. if (!master) {
  275. pr_err("%s: Master is NULL\n", __func__);
  276. return -EINVAL;
  277. }
  278. if (num_port > SWR_MAX_DEV_PORT_NUM) {
  279. dev_err(&master->dev, "%s: num_port %d exceeds max port %d\n",
  280. __func__, num_port, SWR_MAX_DEV_PORT_NUM);
  281. return -EINVAL;
  282. }
  283. /*
  284. * create "txn" to accommodate ports enablement of
  285. * different slave devices calling swr_connect_port at the
  286. * same time. Once master process the txn data, it calls
  287. * swr_port_response() to free the transaction. Maximum
  288. * of 256 transactions can be allocated.
  289. */
  290. txn = kzalloc(sizeof(struct swr_params), GFP_KERNEL);
  291. if (!txn)
  292. return -ENOMEM;
  293. mutex_lock(&master->mlock);
  294. for (i = 0; i < master->last_tid; i++) {
  295. if (master->port_txn[i] == NULL)
  296. break;
  297. }
  298. if (i >= master->last_tid) {
  299. if (master->last_tid == 255) {
  300. mutex_unlock(&master->mlock);
  301. kfree(txn);
  302. dev_err(&master->dev, "%s Max tid reached\n",
  303. __func__);
  304. return -ENOMEM;
  305. }
  306. temp_txn = krealloc(master->port_txn,
  307. (i + 1) * sizeof(struct swr_params *),
  308. GFP_KERNEL);
  309. if (!temp_txn) {
  310. mutex_unlock(&master->mlock);
  311. kfree(txn);
  312. dev_err(&master->dev, "%s Not able to allocate\n"
  313. "master port transaction memory\n",
  314. __func__);
  315. return -ENOMEM;
  316. }
  317. master->port_txn = temp_txn;
  318. master->last_tid++;
  319. }
  320. master->port_txn[i] = txn;
  321. mutex_unlock(&master->mlock);
  322. txn->tid = i;
  323. txn->dev_num = dev->dev_num;
  324. txn->num_port = num_port;
  325. for (i = 0; i < num_port; i++) {
  326. txn->port_id[i] = port_id[i];
  327. txn->num_ch[i] = num_ch[i];
  328. txn->ch_rate[i] = ch_rate[i];
  329. txn->ch_en[i] = ch_mask[i];
  330. txn->port_type[i] = port_type[i];
  331. }
  332. ret = master->connect_port(master, txn);
  333. return ret;
  334. }
  335. EXPORT_SYMBOL(swr_connect_port);
  336. /**
  337. * swr_disconnect_port - disable soundwire slave port(s)
  338. * @dev: pointer to soundwire slave device
  339. * @port_id: logical port id(s) of soundwire slave device
  340. * @num_port: number of slave device ports need to be disabled
  341. *
  342. * soundwire slave device call swr_disconnect_port API to disable all/some of
  343. * its ports. This API will call master disconnect_port callback function to
  344. * disable master and slave port and (re)configure frame structure
  345. */
  346. int swr_disconnect_port(struct swr_device *dev, u8 *port_id, u8 num_port,
  347. u8 *ch_mask, u8 *port_type)
  348. {
  349. u8 i = 0;
  350. int ret;
  351. struct swr_params *txn = NULL;
  352. struct swr_params **temp_txn = NULL;
  353. struct swr_master *master = dev->master;
  354. if (!master) {
  355. pr_err("%s: Master is NULL\n", __func__);
  356. return -EINVAL;
  357. }
  358. if (num_port > SWR_MAX_DEV_PORT_NUM) {
  359. dev_err(&master->dev, "%s: num_port %d exceeds max port %d\n",
  360. __func__, num_port, SWR_MAX_DEV_PORT_NUM);
  361. return -EINVAL;
  362. }
  363. txn = kzalloc(sizeof(struct swr_params), GFP_KERNEL);
  364. if (!txn)
  365. return -ENOMEM;
  366. mutex_lock(&master->mlock);
  367. for (i = 0; i < master->last_tid; i++) {
  368. if (master->port_txn[i] == NULL)
  369. break;
  370. }
  371. if (i >= master->last_tid) {
  372. if (master->last_tid == 255) {
  373. mutex_unlock(&master->mlock);
  374. kfree(txn);
  375. dev_err(&master->dev, "%s Max tid reached\n",
  376. __func__);
  377. return -ENOMEM;
  378. }
  379. temp_txn = krealloc(master->port_txn,
  380. (i + 1) * sizeof(struct swr_params *),
  381. GFP_KERNEL);
  382. if (!temp_txn) {
  383. mutex_unlock(&master->mlock);
  384. kfree(txn);
  385. dev_err(&master->dev, "%s Not able to allocate\n"
  386. "master port transaction memory\n",
  387. __func__);
  388. return -ENOMEM;
  389. }
  390. master->port_txn = temp_txn;
  391. master->last_tid++;
  392. }
  393. master->port_txn[i] = txn;
  394. mutex_unlock(&master->mlock);
  395. txn->tid = i;
  396. txn->dev_num = dev->dev_num;
  397. txn->num_port = num_port;
  398. for (i = 0; i < num_port; i++) {
  399. txn->port_id[i] = port_id[i];
  400. txn->ch_en[i] = ch_mask[i];
  401. txn->port_type[i] = port_type[i];
  402. }
  403. ret = master->disconnect_port(master, txn);
  404. return ret;
  405. }
  406. EXPORT_SYMBOL(swr_disconnect_port);
  407. /**
  408. * swr_get_logical_dev_num - Get soundwire slave logical device number
  409. * @dev: pointer to soundwire slave device
  410. * @dev_id: physical device id of soundwire slave device
  411. * @dev_num: pointer to logical device num of soundwire slave device
  412. *
  413. * This API will get the logical device number of soundwire slave device
  414. */
  415. int swr_get_logical_dev_num(struct swr_device *dev, u64 dev_id,
  416. u8 *dev_num)
  417. {
  418. int ret = 0;
  419. struct swr_master *master = dev->master;
  420. if (!master) {
  421. pr_err("%s: Master is NULL\n", __func__);
  422. return -EINVAL;
  423. }
  424. mutex_lock(&master->mlock);
  425. ret = master->get_logical_dev_num(master, dev_id, dev_num);
  426. if (ret) {
  427. pr_err("%s: Error %d to get logical addr for device %llx\n",
  428. __func__, ret, dev_id);
  429. }
  430. mutex_unlock(&master->mlock);
  431. return ret;
  432. }
  433. EXPORT_SYMBOL(swr_get_logical_dev_num);
  434. /**
  435. * swr_read - read soundwire slave device registers
  436. * @dev: pointer to soundwire slave device
  437. * @dev_num: logical device num of soundwire slave device
  438. * @reg_addr: base register address that needs to be read
  439. * @buf: pointer to store the values of registers from base address
  440. * @len: length of the buffer
  441. *
  442. * This API will read the value of the register address from
  443. * soundwire slave device
  444. */
  445. int swr_read(struct swr_device *dev, u8 dev_num, u16 reg_addr,
  446. void *buf, u32 len)
  447. {
  448. struct swr_master *master = dev->master;
  449. if (!master)
  450. return -EINVAL;
  451. return master->read(master, dev_num, reg_addr, buf, len);
  452. }
  453. EXPORT_SYMBOL(swr_read);
  454. /**
  455. * swr_bulk_write - write soundwire slave device registers
  456. * @dev: pointer to soundwire slave device
  457. * @dev_num: logical device num of soundwire slave device
  458. * @reg_addr: register address of soundwire slave device
  459. * @buf: contains value of register address
  460. * @len: indicates number of registers
  461. *
  462. * This API will write the value of the register address to
  463. * soundwire slave device
  464. */
  465. int swr_bulk_write(struct swr_device *dev, u8 dev_num, void *reg,
  466. const void *buf, size_t len)
  467. {
  468. struct swr_master *master;
  469. if (!dev || !dev->master)
  470. return -EINVAL;
  471. master = dev->master;
  472. if (dev->group_id) {
  473. if (master->gr_sid != dev_num) {
  474. if (!master->gr_sid)
  475. master->gr_sid = dev_num;
  476. else
  477. return 0;
  478. }
  479. dev_num = dev->group_id;
  480. }
  481. if (master->bulk_write)
  482. return master->bulk_write(master, dev_num, reg, buf, len);
  483. return -EOPNOTSUPP;
  484. }
  485. EXPORT_SYMBOL(swr_bulk_write);
  486. /**
  487. * swr_write - write soundwire slave device registers
  488. * @dev: pointer to soundwire slave device
  489. * @dev_num: logical device num of soundwire slave device
  490. * @reg_addr: register address of soundwire slave device
  491. * @buf: contains value of register address
  492. *
  493. * This API will write the value of the register address to
  494. * soundwire slave device
  495. */
  496. int swr_write(struct swr_device *dev, u8 dev_num, u16 reg_addr,
  497. const void *buf)
  498. {
  499. struct swr_master *master = dev->master;
  500. if (!master)
  501. return -EINVAL;
  502. if (dev->group_id) {
  503. if (master->gr_sid != dev_num) {
  504. if (!master->gr_sid)
  505. master->gr_sid = dev_num;
  506. else
  507. return 0;
  508. }
  509. dev_num = dev->group_id;
  510. }
  511. return master->write(master, dev_num, reg_addr, buf);
  512. }
  513. EXPORT_SYMBOL(swr_write);
  514. /**
  515. * swr_device_up - Function to bringup the soundwire slave device
  516. * @swr_dev: pointer to soundwire slave device
  517. * Context: can sleep
  518. *
  519. * This API will be called by soundwire master to bringup the slave
  520. * device.
  521. */
  522. int swr_device_up(struct swr_device *swr_dev)
  523. {
  524. struct device *dev;
  525. const struct swr_driver *sdrv;
  526. if (!swr_dev)
  527. return -EINVAL;
  528. dev = &swr_dev->dev;
  529. sdrv = to_swr_driver(dev->driver);
  530. if (!sdrv)
  531. return 0;
  532. if (sdrv->device_up)
  533. return sdrv->device_up(to_swr_device(dev));
  534. return -ENODEV;
  535. }
  536. EXPORT_SYMBOL(swr_device_up);
  537. /**
  538. * swr_device_down - Function to call soundwire slave device down
  539. * @swr_dev: pointer to soundwire slave device
  540. * Context: can sleep
  541. *
  542. * This API will be called by soundwire master to put slave device in
  543. * shutdown state.
  544. */
  545. int swr_device_down(struct swr_device *swr_dev)
  546. {
  547. struct device *dev;
  548. const struct swr_driver *sdrv;
  549. if (!swr_dev)
  550. return -EINVAL;
  551. dev = &swr_dev->dev;
  552. sdrv = to_swr_driver(dev->driver);
  553. if (!sdrv)
  554. return 0;
  555. if (sdrv->device_down)
  556. return sdrv->device_down(to_swr_device(dev));
  557. return -ENODEV;
  558. }
  559. EXPORT_SYMBOL(swr_device_down);
  560. /**
  561. * swr_reset_device - reset soundwire slave device
  562. * @swr_dev: pointer to soundwire slave device
  563. * Context: can sleep
  564. *
  565. * This API will be called by soundwire master to reset the slave
  566. * device when the slave device is not responding or in undefined
  567. * state
  568. */
  569. int swr_reset_device(struct swr_device *swr_dev)
  570. {
  571. struct device *dev;
  572. const struct swr_driver *sdrv;
  573. if (!swr_dev)
  574. return -EINVAL;
  575. dev = &swr_dev->dev;
  576. sdrv = to_swr_driver(dev->driver);
  577. if (!sdrv)
  578. return -EINVAL;
  579. if (sdrv->reset_device)
  580. return sdrv->reset_device(to_swr_device(dev));
  581. return -ENODEV;
  582. }
  583. EXPORT_SYMBOL(swr_reset_device);
  584. /**
  585. * swr_set_device_group - Assign group id to the slave devices
  586. * @swr_dev: pointer to soundwire slave device
  587. * @id: group id to be assigned to slave device
  588. * Context: can sleep
  589. *
  590. * This API will be called either from soundwire master or slave
  591. * device to assign group id.
  592. */
  593. int swr_set_device_group(struct swr_device *swr_dev, u8 id)
  594. {
  595. struct swr_master *master;
  596. if (!swr_dev)
  597. return -EINVAL;
  598. swr_dev->group_id = id;
  599. master = swr_dev->master;
  600. if (!id && master)
  601. master->gr_sid = 0;
  602. return 0;
  603. }
  604. EXPORT_SYMBOL(swr_set_device_group);
  605. static int swr_drv_probe(struct device *dev)
  606. {
  607. const struct swr_driver *sdrv = to_swr_driver(dev->driver);
  608. if (!sdrv)
  609. return -EINVAL;
  610. if (sdrv->probe)
  611. return sdrv->probe(to_swr_device(dev));
  612. return -ENODEV;
  613. }
  614. static int swr_drv_remove(struct device *dev)
  615. {
  616. const struct swr_driver *sdrv = to_swr_driver(dev->driver);
  617. if (!sdrv)
  618. return -EINVAL;
  619. if (sdrv->remove)
  620. return sdrv->remove(to_swr_device(dev));
  621. return -ENODEV;
  622. }
  623. static void swr_drv_shutdown(struct device *dev)
  624. {
  625. const struct swr_driver *sdrv = to_swr_driver(dev->driver);
  626. if (!sdrv)
  627. return;
  628. if (sdrv->shutdown)
  629. sdrv->shutdown(to_swr_device(dev));
  630. }
  631. /**
  632. * swr_driver_register - register a soundwire driver
  633. * @drv: the driver to register
  634. * Context: can sleep
  635. */
  636. int swr_driver_register(struct swr_driver *drv)
  637. {
  638. drv->driver.bus = &soundwire_type;
  639. if (drv->probe)
  640. drv->driver.probe = swr_drv_probe;
  641. if (drv->remove)
  642. drv->driver.remove = swr_drv_remove;
  643. if (drv->shutdown)
  644. drv->driver.shutdown = swr_drv_shutdown;
  645. return driver_register(&drv->driver);
  646. }
  647. EXPORT_SYMBOL(swr_driver_register);
  648. /**
  649. * swr_driver_unregister - unregister a soundwire driver
  650. * @drv: the driver to unregister
  651. */
  652. void swr_driver_unregister(struct swr_driver *drv)
  653. {
  654. if (drv)
  655. driver_unregister(&drv->driver);
  656. }
  657. EXPORT_SYMBOL(swr_driver_unregister);
  658. static void swr_match_ctrl_to_boardinfo(struct swr_master *master,
  659. struct swr_boardinfo *bi)
  660. {
  661. struct swr_device *swr;
  662. if (master->bus_num != bi->bus_num) {
  663. dev_dbg(&master->dev,
  664. "%s: master# %d and bi# %d does not match\n",
  665. __func__, master->bus_num, bi->bus_num);
  666. return;
  667. }
  668. swr = swr_new_device(master, bi);
  669. if (!swr)
  670. dev_err(&master->dev, "can't create new device for %s\n",
  671. bi->swr_slave->name);
  672. }
  673. /**
  674. * swr_master_add_boarddevices - Add devices registered by board info
  675. * @master: master to which these devices are to be added to.
  676. *
  677. * This API is called by master when it is up and running. If devices
  678. * on a master were registered before master, this will make sure that
  679. * they get probed when master is up.
  680. */
  681. void swr_master_add_boarddevices(struct swr_master *master)
  682. {
  683. struct boardinfo *bi;
  684. mutex_lock(&board_lock);
  685. list_add_tail(&master->list, &swr_master_list);
  686. list_for_each_entry(bi, &board_list, list)
  687. swr_match_ctrl_to_boardinfo(master, &bi->board_info);
  688. mutex_unlock(&board_lock);
  689. }
  690. EXPORT_SYMBOL(swr_master_add_boarddevices);
  691. struct swr_device *get_matching_swr_slave_device(struct device_node *np)
  692. {
  693. struct swr_device *swr = NULL;
  694. struct swr_master *master;
  695. bool found = false;
  696. mutex_lock(&board_lock);
  697. list_for_each_entry(master, &swr_master_list, list) {
  698. mutex_lock(&master->mlock);
  699. list_for_each_entry(swr, &master->devices, dev_list) {
  700. if (swr->dev.of_node == np) {
  701. found = true;
  702. mutex_unlock(&master->mlock);
  703. goto exit;
  704. }
  705. }
  706. mutex_unlock(&master->mlock);
  707. }
  708. exit:
  709. mutex_unlock(&board_lock);
  710. if (!found)
  711. return NULL;
  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");