swr-mstr-ctrl.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850
  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/irq.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/kthread.h>
  21. #include <linux/clk.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/of.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/uaccess.h>
  26. #include <soc/soundwire.h>
  27. #include <soc/swr-wcd.h>
  28. #include <linux/regmap.h>
  29. #include "swrm_registers.h"
  30. #include "swr-mstr-ctrl.h"
  31. #include "swrm_port_config.h"
  32. #define SWR_BROADCAST_CMD_ID 0x0F
  33. #define SWR_AUTO_SUSPEND_DELAY 3 /* delay in sec */
  34. #define SWR_DEV_ID_MASK 0xFFFFFFFF
  35. #define SWR_REG_VAL_PACK(data, dev, id, reg) \
  36. ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
  37. /* pm runtime auto suspend timer in msecs */
  38. static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
  39. module_param(auto_suspend_timer, int, 0664);
  40. MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
  41. enum {
  42. SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
  43. SWR_ATTACHED_OK, /* Device is attached */
  44. SWR_ALERT, /* Device alters master for any interrupts */
  45. SWR_RESERVED, /* Reserved */
  46. };
  47. enum {
  48. MASTER_ID_WSA = 1,
  49. MASTER_ID_RX,
  50. MASTER_ID_TX
  51. };
  52. #define MASTER_ID_MASK 0xF
  53. #define TRUE 1
  54. #define FALSE 0
  55. #define SWRM_MAX_PORT_REG 40
  56. #define SWRM_MAX_INIT_REG 8
  57. #define SWR_MSTR_MAX_REG_ADDR 0x1740
  58. #define SWR_MSTR_START_REG_ADDR 0x00
  59. #define SWR_MSTR_MAX_BUF_LEN 32
  60. #define BYTES_PER_LINE 12
  61. #define SWR_MSTR_RD_BUF_LEN 8
  62. #define SWR_MSTR_WR_BUF_LEN 32
  63. static struct swr_mstr_ctrl *dbgswrm;
  64. static struct dentry *debugfs_swrm_dent;
  65. static struct dentry *debugfs_peek;
  66. static struct dentry *debugfs_poke;
  67. static struct dentry *debugfs_reg_dump;
  68. static unsigned int read_data;
  69. static bool swrm_is_msm_variant(int val)
  70. {
  71. return (val == SWRM_VERSION_1_3);
  72. }
  73. static int swrm_debug_open(struct inode *inode, struct file *file)
  74. {
  75. file->private_data = inode->i_private;
  76. return 0;
  77. }
  78. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  79. {
  80. char *token;
  81. int base, cnt;
  82. token = strsep(&buf, " ");
  83. for (cnt = 0; cnt < num_of_par; cnt++) {
  84. if (token) {
  85. if ((token[1] == 'x') || (token[1] == 'X'))
  86. base = 16;
  87. else
  88. base = 10;
  89. if (kstrtou32(token, base, &param1[cnt]) != 0)
  90. return -EINVAL;
  91. token = strsep(&buf, " ");
  92. } else
  93. return -EINVAL;
  94. }
  95. return 0;
  96. }
  97. static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
  98. loff_t *ppos)
  99. {
  100. int i, reg_val, len;
  101. ssize_t total = 0;
  102. char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
  103. if (!ubuf || !ppos)
  104. return 0;
  105. for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
  106. i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
  107. reg_val = dbgswrm->read(dbgswrm->handle, i);
  108. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
  109. if ((total + len) >= count - 1)
  110. break;
  111. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  112. pr_err("%s: fail to copy reg dump\n", __func__);
  113. total = -EFAULT;
  114. goto copy_err;
  115. }
  116. *ppos += len;
  117. total += len;
  118. }
  119. copy_err:
  120. return total;
  121. }
  122. static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
  123. size_t count, loff_t *ppos)
  124. {
  125. char lbuf[SWR_MSTR_RD_BUF_LEN];
  126. char *access_str;
  127. ssize_t ret_cnt;
  128. if (!count || !file || !ppos || !ubuf)
  129. return -EINVAL;
  130. access_str = file->private_data;
  131. if (*ppos < 0)
  132. return -EINVAL;
  133. if (!strcmp(access_str, "swrm_peek")) {
  134. snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
  135. ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
  136. strnlen(lbuf, 7));
  137. } else if (!strcmp(access_str, "swrm_reg_dump")) {
  138. ret_cnt = swrm_reg_show(ubuf, count, ppos);
  139. } else {
  140. pr_err("%s: %s not permitted to read\n", __func__, access_str);
  141. ret_cnt = -EPERM;
  142. }
  143. return ret_cnt;
  144. }
  145. static ssize_t swrm_debug_write(struct file *filp,
  146. const char __user *ubuf, size_t cnt, loff_t *ppos)
  147. {
  148. char lbuf[SWR_MSTR_WR_BUF_LEN];
  149. int rc;
  150. u32 param[5];
  151. char *access_str;
  152. if (!filp || !ppos || !ubuf)
  153. return -EINVAL;
  154. access_str = filp->private_data;
  155. if (cnt > sizeof(lbuf) - 1)
  156. return -EINVAL;
  157. rc = copy_from_user(lbuf, ubuf, cnt);
  158. if (rc)
  159. return -EFAULT;
  160. lbuf[cnt] = '\0';
  161. if (!strcmp(access_str, "swrm_poke")) {
  162. /* write */
  163. rc = get_parameters(lbuf, param, 2);
  164. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
  165. (param[1] <= 0xFFFFFFFF) &&
  166. (rc == 0))
  167. rc = dbgswrm->write(dbgswrm->handle, param[0],
  168. param[1]);
  169. else
  170. rc = -EINVAL;
  171. } else if (!strcmp(access_str, "swrm_peek")) {
  172. /* read */
  173. rc = get_parameters(lbuf, param, 1);
  174. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
  175. read_data = dbgswrm->read(dbgswrm->handle, param[0]);
  176. else
  177. rc = -EINVAL;
  178. }
  179. if (rc == 0)
  180. rc = cnt;
  181. else
  182. pr_err("%s: rc = %d\n", __func__, rc);
  183. return rc;
  184. }
  185. static const struct file_operations swrm_debug_ops = {
  186. .open = swrm_debug_open,
  187. .write = swrm_debug_write,
  188. .read = swrm_debug_read,
  189. };
  190. static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
  191. {
  192. if (!swrm->clk || !swrm->handle)
  193. return -EINVAL;
  194. if (enable) {
  195. swrm->clk_ref_count++;
  196. if (swrm->clk_ref_count == 1) {
  197. swrm->clk(swrm->handle, true);
  198. swrm->state = SWR_MSTR_UP;
  199. }
  200. } else if (--swrm->clk_ref_count == 0) {
  201. swrm->clk(swrm->handle, false);
  202. swrm->state = SWR_MSTR_DOWN;
  203. } else if (swrm->clk_ref_count < 0) {
  204. pr_err("%s: swrm clk count mismatch\n", __func__);
  205. swrm->clk_ref_count = 0;
  206. }
  207. return 0;
  208. }
  209. static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
  210. u16 reg, u32 *value)
  211. {
  212. u32 temp = (u32)(*value) & 0x000000FF;
  213. int ret;
  214. ret = swrm_clk_request(swrm, TRUE);
  215. if (ret)
  216. return -EINVAL;
  217. iowrite32(temp, swrm->swrm_dig_base + reg);
  218. swrm_clk_request(swrm, TRUE);
  219. return 0;
  220. }
  221. static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
  222. u16 reg, u32 *value)
  223. {
  224. u32 temp;
  225. int ret;
  226. ret = swrm_clk_request(swrm, TRUE);
  227. if (ret)
  228. return -EINVAL;
  229. temp = ioread32(swrm->swrm_dig_base + reg);
  230. *value = (u8)temp;
  231. swrm_clk_request(swrm, FALSE);
  232. return 0;
  233. }
  234. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
  235. {
  236. u32 val = 0;
  237. if (swrm->read)
  238. val = swrm->read(swrm->handle, reg_addr);
  239. else
  240. swrm_ahb_read(swrm, reg_addr, &val);
  241. return val;
  242. }
  243. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
  244. {
  245. if (swrm->write)
  246. swrm->write(swrm->handle, reg_addr, val);
  247. else
  248. swrm_ahb_write(swrm, reg_addr, &val);
  249. }
  250. static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
  251. u32 *val, unsigned int length)
  252. {
  253. int i = 0;
  254. if (swrm->bulk_write)
  255. swrm->bulk_write(swrm->handle, reg_addr, val, length);
  256. else {
  257. for (i = 0; i < length; i++)
  258. swr_master_write(swrm, reg_addr[i], val[i]);
  259. }
  260. return 0;
  261. }
  262. static bool swrm_is_port_en(struct swr_master *mstr)
  263. {
  264. return !!(mstr->num_port);
  265. }
  266. static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
  267. {
  268. u8 master_device_id;
  269. int i;
  270. if (swrm->version == SWRM_VERSION_1_5)
  271. master_device_id = swr_master_read(swrm, SWRM_COMP_MASTER_ID);
  272. else
  273. master_device_id = MASTER_ID_WSA;
  274. switch (master_device_id & MASTER_ID_MASK) {
  275. case MASTER_ID_WSA:
  276. /* get port params for wsa */
  277. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  278. /* wsa uses single frame structure for all configurations */
  279. if (!swrm->mport_cfg[i].port_en)
  280. continue;
  281. swrm->mport_cfg[i].sinterval = wsa_frame_superset[i].si;
  282. swrm->mport_cfg[i].offset1 = wsa_frame_superset[i].off1;
  283. swrm->mport_cfg[i].offset2 = wsa_frame_superset[i].off2;
  284. }
  285. break;
  286. case MASTER_ID_RX:
  287. /* get port params for rx */
  288. break;
  289. case MASTER_ID_TX:
  290. /* get port params for tx */
  291. break;
  292. default: /* MASTER_GENERIC*/
  293. /* computer generic frame parameters */
  294. return -EINVAL;
  295. }
  296. return 0;
  297. }
  298. static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
  299. u8 *mstr_ch_mask, u8 mstr_prt_type,
  300. u8 slv_port_id)
  301. {
  302. int i, j;
  303. *mstr_port_id = 0;
  304. for (i = 1; i <= swrm->num_ports; i++) {
  305. for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
  306. if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
  307. goto found;
  308. }
  309. }
  310. found:
  311. if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
  312. dev_err(swrm->dev, "%s: port type not supported by master\n",
  313. __func__);
  314. return -EINVAL;
  315. }
  316. /* id 0 corresponds to master port 1 */
  317. *mstr_port_id = i - 1;
  318. *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
  319. return 0;
  320. }
  321. static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
  322. u8 dev_addr, u16 reg_addr)
  323. {
  324. u32 val;
  325. u8 id = *cmd_id;
  326. if (id != SWR_BROADCAST_CMD_ID) {
  327. if (id < 14)
  328. id += 1;
  329. else
  330. id = 0;
  331. *cmd_id = id;
  332. }
  333. val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
  334. return val;
  335. }
  336. static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
  337. u8 dev_addr, u8 cmd_id, u16 reg_addr,
  338. u32 len)
  339. {
  340. u32 val;
  341. val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
  342. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
  343. *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
  344. dev_dbg(swrm->dev,
  345. "%s: reg: 0x%x, cmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  346. __func__, reg_addr, cmd_id, dev_addr, *cmd_data);
  347. return 0;
  348. }
  349. static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
  350. u8 dev_addr, u8 cmd_id, u16 reg_addr)
  351. {
  352. u32 val;
  353. int ret = 0;
  354. if (!cmd_id)
  355. val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
  356. dev_addr, reg_addr);
  357. else
  358. val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
  359. dev_addr, reg_addr);
  360. dev_dbg(swrm->dev,
  361. "%s: reg: 0x%x, cmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  362. __func__, reg_addr, cmd_id, dev_addr, cmd_data);
  363. swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
  364. if (cmd_id == 0xF) {
  365. /*
  366. * sleep for 10ms for MSM soundwire variant to allow broadcast
  367. * command to complete.
  368. */
  369. if (swrm_is_msm_variant(swrm->version))
  370. usleep_range(10000, 10100);
  371. else
  372. wait_for_completion_timeout(&swrm->broadcast,
  373. (2 * HZ/10));
  374. }
  375. return ret;
  376. }
  377. static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
  378. void *buf, u32 len)
  379. {
  380. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  381. int ret = 0;
  382. int val;
  383. u8 *reg_val = (u8 *)buf;
  384. if (!swrm) {
  385. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  386. return -EINVAL;
  387. }
  388. if (dev_num)
  389. ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr,
  390. len);
  391. else
  392. val = swr_master_read(swrm, reg_addr);
  393. if (!ret)
  394. *reg_val = (u8)val;
  395. pm_runtime_mark_last_busy(swrm->dev);
  396. return ret;
  397. }
  398. static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
  399. const void *buf)
  400. {
  401. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  402. int ret = 0;
  403. u8 reg_val = *(u8 *)buf;
  404. if (!swrm) {
  405. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  406. return -EINVAL;
  407. }
  408. if (dev_num)
  409. ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
  410. else
  411. swr_master_write(swrm, reg_addr, reg_val);
  412. pm_runtime_mark_last_busy(swrm->dev);
  413. return ret;
  414. }
  415. static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
  416. const void *buf, size_t len)
  417. {
  418. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  419. int ret = 0;
  420. int i;
  421. u32 *val;
  422. u32 *swr_fifo_reg;
  423. if (!swrm || !swrm->handle) {
  424. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  425. return -EINVAL;
  426. }
  427. if (len <= 0)
  428. return -EINVAL;
  429. if (dev_num) {
  430. swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
  431. if (!swr_fifo_reg) {
  432. ret = -ENOMEM;
  433. goto err;
  434. }
  435. val = kcalloc(len, sizeof(u32), GFP_KERNEL);
  436. if (!val) {
  437. ret = -ENOMEM;
  438. goto mem_fail;
  439. }
  440. for (i = 0; i < len; i++) {
  441. val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
  442. ((u8 *)buf)[i],
  443. dev_num,
  444. ((u16 *)reg)[i]);
  445. swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
  446. }
  447. ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
  448. if (ret) {
  449. dev_err(&master->dev, "%s: bulk write failed\n",
  450. __func__);
  451. ret = -EINVAL;
  452. }
  453. } else {
  454. dev_err(&master->dev,
  455. "%s: No support of Bulk write for master regs\n",
  456. __func__);
  457. ret = -EINVAL;
  458. goto err;
  459. }
  460. kfree(val);
  461. mem_fail:
  462. kfree(swr_fifo_reg);
  463. err:
  464. pm_runtime_mark_last_busy(swrm->dev);
  465. return ret;
  466. }
  467. static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
  468. {
  469. return (swr_master_read(swrm, SWRM_MCP_STATUS) &
  470. SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
  471. }
  472. static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
  473. u8 row, u8 col)
  474. {
  475. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  476. SWRS_SCP_FRAME_CTRL_BANK(bank));
  477. }
  478. static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
  479. u8 slv_port, u8 dev_num)
  480. {
  481. struct swr_port_info *port_req = NULL;
  482. list_for_each_entry(port_req, &mport->port_req_list, list) {
  483. /* Store dev_id instead of dev_num if enumeration is changed run_time */
  484. if ((port_req->slave_port_id == slv_port)
  485. && (port_req->dev_num == dev_num))
  486. return port_req;
  487. }
  488. return NULL;
  489. }
  490. static bool swrm_remove_from_group(struct swr_master *master)
  491. {
  492. struct swr_device *swr_dev;
  493. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  494. bool is_removed = false;
  495. if (!swrm)
  496. goto end;
  497. mutex_lock(&swrm->mlock);
  498. if ((swrm->num_rx_chs > 1) &&
  499. (swrm->num_rx_chs == swrm->num_cfg_devs)) {
  500. list_for_each_entry(swr_dev, &master->devices,
  501. dev_list) {
  502. swr_dev->group_id = SWR_GROUP_NONE;
  503. master->gr_sid = 0;
  504. }
  505. is_removed = true;
  506. }
  507. mutex_unlock(&swrm->mlock);
  508. end:
  509. return is_removed;
  510. }
  511. static void swrm_disable_ports(struct swr_master *master,
  512. u8 bank)
  513. {
  514. u32 value;
  515. struct swr_port_info *port_req;
  516. int i;
  517. struct swrm_mports *mport;
  518. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  519. if (!swrm) {
  520. pr_err("%s: swrm is null\n", __func__);
  521. return;
  522. }
  523. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  524. master->num_port);
  525. for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
  526. mport = &(swrm->mport_cfg[i]);
  527. if (!mport->port_en)
  528. continue;
  529. list_for_each_entry(port_req, &mport->port_req_list, list) {
  530. /* skip ports with no change req's*/
  531. if (port_req->req_ch == port_req->ch_en)
  532. continue;
  533. swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
  534. port_req->dev_num, 0x00,
  535. SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
  536. bank));
  537. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
  538. __func__, i,
  539. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
  540. }
  541. value = ((mport->req_ch)
  542. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  543. value |= ((mport->offset2)
  544. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  545. value |= ((mport->offset1)
  546. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  547. value |= mport->sinterval;
  548. swr_master_write(swrm,
  549. SWRM_DP_PORT_CTRL_BANK(i+1, bank),
  550. value);
  551. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  552. __func__, i,
  553. (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
  554. }
  555. }
  556. static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
  557. {
  558. struct swr_port_info *port_req, *next;
  559. int i;
  560. struct swrm_mports *mport;
  561. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  562. if (!swrm) {
  563. pr_err("%s: swrm is null\n", __func__);
  564. return;
  565. }
  566. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  567. master->num_port);
  568. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  569. mport = &(swrm->mport_cfg[i]);
  570. list_for_each_entry_safe(port_req, next,
  571. &mport->port_req_list, list) {
  572. /* skip ports without new ch req */
  573. if (port_req->ch_en == port_req->req_ch)
  574. continue;
  575. /* remove new ch req's*/
  576. port_req->req_ch = port_req->ch_en;
  577. /* If no streams enabled on port, remove the port req */
  578. if (port_req->ch_en == 0) {
  579. list_del(&port_req->list);
  580. kfree(port_req);
  581. }
  582. }
  583. /* remove new ch req's on mport*/
  584. mport->req_ch = mport->ch_en;
  585. if (!(mport->ch_en)) {
  586. mport->port_en = false;
  587. master->port_en_mask &= ~i;
  588. }
  589. }
  590. }
  591. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  592. {
  593. u32 value, slv_id;
  594. struct swr_port_info *port_req;
  595. int i;
  596. struct swrm_mports *mport;
  597. u32 reg[SWRM_MAX_PORT_REG];
  598. u32 val[SWRM_MAX_PORT_REG];
  599. int len = 0;
  600. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  601. if (!swrm) {
  602. pr_err("%s: swrm is null\n", __func__);
  603. return;
  604. }
  605. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  606. master->num_port);
  607. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  608. mport = &(swrm->mport_cfg[i]);
  609. if (!mport->port_en)
  610. continue;
  611. list_for_each_entry(port_req, &mport->port_req_list, list) {
  612. slv_id = port_req->slave_port_id;
  613. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  614. val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
  615. port_req->dev_num, 0x00,
  616. SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
  617. bank));
  618. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  619. val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
  620. port_req->dev_num, 0x00,
  621. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  622. bank));
  623. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  624. val[len++] = SWR_REG_VAL_PACK(mport->offset1,
  625. port_req->dev_num, 0x00,
  626. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  627. bank));
  628. if (port_req->slave_port_id) {
  629. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  630. val[len++] = SWR_REG_VAL_PACK(mport->offset2,
  631. port_req->dev_num, 0x00,
  632. SWRS_DP_OFFSET_CONTROL_2_BANK(
  633. slv_id, bank));
  634. }
  635. port_req->ch_en = port_req->req_ch;
  636. }
  637. value = ((mport->req_ch)
  638. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  639. value |= ((mport->offset2)
  640. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  641. value |= ((mport->offset1)
  642. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  643. value |= mport->sinterval;
  644. reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
  645. val[len++] = value;
  646. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  647. __func__, i,
  648. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
  649. mport->ch_en = mport->req_ch;
  650. }
  651. swr_master_bulk_write(swrm, reg, val, len);
  652. }
  653. static void swrm_apply_port_config(struct swr_master *master)
  654. {
  655. u8 bank;
  656. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  657. if (!swrm) {
  658. pr_err("%s: Invalid handle to swr controller\n",
  659. __func__);
  660. return;
  661. }
  662. bank = get_inactive_bank_num(swrm);
  663. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  664. __func__, bank, master->num_port);
  665. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
  666. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  667. swrm_copy_data_port_config(master, bank);
  668. }
  669. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  670. {
  671. u8 bank;
  672. u32 value, n_col;
  673. int ret;
  674. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  675. int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
  676. SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
  677. SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
  678. u8 inactive_bank;
  679. if (!swrm) {
  680. pr_err("%s: swrm is null\n", __func__);
  681. return -EFAULT;
  682. }
  683. mutex_lock(&swrm->mlock);
  684. /* wakeup soundwire master if in sleep */
  685. pm_runtime_get_sync(swrm->dev);
  686. bank = get_inactive_bank_num(swrm);
  687. if (enable) {
  688. ret = swrm_get_port_config(swrm);
  689. if (ret) {
  690. /* cannot accommodate ports */
  691. swrm_cleanup_disabled_port_reqs(master);
  692. mutex_unlock(&swrm->mlock);
  693. return -EINVAL;
  694. }
  695. /* apply the new port config*/
  696. swrm_apply_port_config(master);
  697. } else {
  698. swrm_disable_ports(master, bank);
  699. }
  700. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
  701. __func__, enable, swrm->num_cfg_devs);
  702. if (enable) {
  703. /* set Row = 48 and col = 16 */
  704. n_col = SWR_MAX_COL;
  705. } else {
  706. /*
  707. * Do not change to 48x2 if there are still active ports
  708. */
  709. if (!master->num_port)
  710. n_col = SWR_MIN_COL;
  711. else
  712. n_col = SWR_MAX_COL;
  713. }
  714. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
  715. value &= (~mask);
  716. value |= ((0 << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  717. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  718. (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  719. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  720. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  721. SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  722. enable_bank_switch(swrm, bank, SWR_MAX_ROW, n_col);
  723. inactive_bank = bank ? 0 : 1;
  724. if (enable)
  725. swrm_copy_data_port_config(master, inactive_bank);
  726. else {
  727. swrm_disable_ports(master, inactive_bank);
  728. swrm_cleanup_disabled_port_reqs(master);
  729. }
  730. if (!swrm_is_port_en(master)) {
  731. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  732. __func__);
  733. pm_runtime_mark_last_busy(swrm->dev);
  734. pm_runtime_put_autosuspend(swrm->dev);
  735. }
  736. mutex_unlock(&swrm->mlock);
  737. return 0;
  738. }
  739. static int swrm_connect_port(struct swr_master *master,
  740. struct swr_params *portinfo)
  741. {
  742. int i;
  743. struct swr_port_info *port_req;
  744. int ret = 0;
  745. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  746. struct swrm_mports *mport;
  747. u8 mstr_port_id, mstr_ch_msk;
  748. dev_dbg(&master->dev, "%s: enter\n", __func__);
  749. if (!portinfo)
  750. return -EINVAL;
  751. if (!swrm) {
  752. dev_err(&master->dev,
  753. "%s: Invalid handle to swr controller\n",
  754. __func__);
  755. return -EINVAL;
  756. }
  757. mutex_lock(&swrm->mlock);
  758. for (i = 0; i < portinfo->num_port; i++) {
  759. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  760. portinfo->port_type[i],
  761. portinfo->port_id[i]);
  762. if (ret) {
  763. dev_err(&master->dev,
  764. "%s: mstr portid for slv port %d not found\n",
  765. __func__, portinfo->port_id[i]);
  766. goto port_fail;
  767. }
  768. mport = &(swrm->mport_cfg[mstr_port_id]);
  769. /* get port req */
  770. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  771. portinfo->dev_num);
  772. if (!port_req) {
  773. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  774. __func__, portinfo->port_id[i],
  775. portinfo->dev_num);
  776. port_req = kzalloc(sizeof(struct swr_port_info),
  777. GFP_KERNEL);
  778. if (!port_req) {
  779. ret = -ENOMEM;
  780. goto mem_fail;
  781. }
  782. port_req->dev_num = portinfo->dev_num;
  783. port_req->slave_port_id = portinfo->port_id[i];
  784. port_req->num_ch = portinfo->num_ch[i];
  785. port_req->ch_rate = portinfo->ch_rate[i];
  786. port_req->ch_en = 0;
  787. port_req->master_port_id = mstr_port_id;
  788. list_add(&port_req->list, &mport->port_req_list);
  789. }
  790. port_req->req_ch |= portinfo->ch_en[i];
  791. dev_dbg(&master->dev,
  792. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  793. __func__, port_req->master_port_id,
  794. port_req->slave_port_id, port_req->ch_rate,
  795. port_req->num_ch);
  796. /* Put the port req on master port */
  797. mport = &(swrm->mport_cfg[mstr_port_id]);
  798. mport->port_en = true;
  799. mport->req_ch |= mstr_ch_msk;
  800. master->port_en_mask |= (1 << mstr_port_id);
  801. }
  802. master->num_port += portinfo->num_port;
  803. swr_port_response(master, portinfo->tid);
  804. mutex_unlock(&swrm->mlock);
  805. return 0;
  806. port_fail:
  807. mem_fail:
  808. /* cleanup port reqs in error condition */
  809. swrm_cleanup_disabled_port_reqs(master);
  810. mutex_unlock(&swrm->mlock);
  811. return ret;
  812. }
  813. static int swrm_disconnect_port(struct swr_master *master,
  814. struct swr_params *portinfo)
  815. {
  816. int i, ret = 0;
  817. struct swr_port_info *port_req;
  818. struct swrm_mports *mport;
  819. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  820. u8 mstr_port_id, mstr_ch_mask;
  821. if (!swrm) {
  822. dev_err(&master->dev,
  823. "%s: Invalid handle to swr controller\n",
  824. __func__);
  825. return -EINVAL;
  826. }
  827. if (!portinfo) {
  828. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  829. return -EINVAL;
  830. }
  831. mutex_lock(&swrm->mlock);
  832. for (i = 0; i < portinfo->num_port; i++) {
  833. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  834. portinfo->port_type[i], portinfo->port_id[i]);
  835. if (ret) {
  836. dev_err(&master->dev,
  837. "%s: mstr portid for slv port %d not found\n",
  838. __func__, portinfo->port_id[i]);
  839. mutex_unlock(&swrm->mlock);
  840. return -EINVAL;
  841. }
  842. mport = &(swrm->mport_cfg[mstr_port_id]);
  843. /* get port req */
  844. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  845. portinfo->dev_num);
  846. if (!port_req) {
  847. dev_err(&master->dev, "%s:port not enabled : port %d\n",
  848. __func__, portinfo->port_id[i]);
  849. return -EINVAL;
  850. }
  851. port_req->req_ch &= ~portinfo->ch_en[i];
  852. mport->req_ch &= ~mstr_ch_mask;
  853. }
  854. master->num_port -= portinfo->num_port;
  855. swr_port_response(master, portinfo->tid);
  856. mutex_unlock(&swrm->mlock);
  857. return 0;
  858. }
  859. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  860. int status, u8 *devnum)
  861. {
  862. int i;
  863. int new_sts = status;
  864. int ret = SWR_NOT_PRESENT;
  865. if (status != swrm->slave_status) {
  866. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  867. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  868. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  869. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  870. *devnum = i;
  871. break;
  872. }
  873. status >>= 2;
  874. swrm->slave_status >>= 2;
  875. }
  876. swrm->slave_status = new_sts;
  877. }
  878. return ret;
  879. }
  880. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  881. {
  882. struct swr_mstr_ctrl *swrm = dev;
  883. u32 value, intr_sts;
  884. int status, chg_sts, i;
  885. u8 devnum = 0;
  886. int ret = IRQ_HANDLED;
  887. struct swr_device *swr_dev;
  888. struct swr_master *mstr = &swrm->master;
  889. mutex_lock(&swrm->reslock);
  890. swrm_clk_request(swrm, true);
  891. mutex_unlock(&swrm->reslock);
  892. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  893. intr_sts &= SWRM_INTERRUPT_STATUS_RMSK;
  894. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  895. value = intr_sts & (1 << i);
  896. if (!value)
  897. continue;
  898. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, value);
  899. switch (value) {
  900. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  901. dev_dbg(swrm->dev, "Trigger irq to slave device\n");
  902. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  903. swrm_check_slave_change_status(swrm, status,
  904. &devnum);
  905. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  906. if (swr_dev->dev_num != devnum)
  907. continue;
  908. if (swr_dev->slave_irq)
  909. handle_nested_irq(swr_dev->slave_irq);
  910. }
  911. break;
  912. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  913. dev_dbg(swrm->dev, "SWR new slave attached\n");
  914. break;
  915. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  916. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  917. if (status == swrm->slave_status) {
  918. dev_dbg(swrm->dev,
  919. "%s: No change in slave status: %d\n",
  920. __func__, status);
  921. break;
  922. }
  923. chg_sts = swrm_check_slave_change_status(swrm, status,
  924. &devnum);
  925. switch (chg_sts) {
  926. case SWR_NOT_PRESENT:
  927. dev_dbg(swrm->dev, "device %d got detached\n",
  928. devnum);
  929. break;
  930. case SWR_ATTACHED_OK:
  931. dev_dbg(swrm->dev, "device %d got attached\n",
  932. devnum);
  933. break;
  934. case SWR_ALERT:
  935. dev_dbg(swrm->dev,
  936. "device %d has pending interrupt\n",
  937. devnum);
  938. break;
  939. }
  940. break;
  941. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  942. dev_err_ratelimited(swrm->dev,
  943. "SWR bus clsh detected\n");
  944. break;
  945. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  946. dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
  947. break;
  948. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  949. dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
  950. break;
  951. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  952. dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
  953. break;
  954. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  955. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  956. dev_err_ratelimited(swrm->dev,
  957. "SWR CMD error, fifo status 0x%x, flushing fifo\n",
  958. value);
  959. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  960. break;
  961. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  962. dev_dbg(swrm->dev, "SWR Port collision detected\n");
  963. break;
  964. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  965. dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
  966. break;
  967. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  968. complete(&swrm->broadcast);
  969. dev_dbg(swrm->dev, "SWR cmd id finished\n");
  970. break;
  971. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
  972. break;
  973. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  974. break;
  975. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  976. break;
  977. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  978. complete(&swrm->reset);
  979. break;
  980. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  981. break;
  982. default:
  983. dev_err_ratelimited(swrm->dev,
  984. "SWR unknown interrupt\n");
  985. ret = IRQ_NONE;
  986. break;
  987. }
  988. }
  989. mutex_lock(&swrm->reslock);
  990. swrm_clk_request(swrm, false);
  991. mutex_unlock(&swrm->reslock);
  992. return ret;
  993. }
  994. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  995. {
  996. u32 val;
  997. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  998. val = (swrm->slave_status >> (devnum * 2));
  999. val &= SWRM_MCP_SLV_STATUS_MASK;
  1000. return val;
  1001. }
  1002. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1003. u8 *dev_num)
  1004. {
  1005. int i;
  1006. u64 id = 0;
  1007. int ret = -EINVAL;
  1008. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1009. struct swr_device *swr_dev;
  1010. u32 num_dev = 0;
  1011. if (!swrm) {
  1012. pr_err("%s: Invalid handle to swr controller\n",
  1013. __func__);
  1014. return ret;
  1015. }
  1016. if (swrm->num_dev)
  1017. num_dev = swrm->num_dev;
  1018. else
  1019. num_dev = mstr->num_dev;
  1020. pm_runtime_get_sync(swrm->dev);
  1021. for (i = 1; i < (num_dev + 1); i++) {
  1022. id = ((u64)(swr_master_read(swrm,
  1023. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1024. id |= swr_master_read(swrm,
  1025. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1026. /*
  1027. * As pm_runtime_get_sync() brings all slaves out of reset
  1028. * update logical device number for all slaves.
  1029. */
  1030. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1031. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1032. u32 status = swrm_get_device_status(swrm, i);
  1033. if ((status == 0x01) || (status == 0x02)) {
  1034. swr_dev->dev_num = i;
  1035. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1036. *dev_num = i;
  1037. ret = 0;
  1038. }
  1039. dev_dbg(swrm->dev,
  1040. "%s: devnum %d is assigned for dev addr %lx\n",
  1041. __func__, i, swr_dev->addr);
  1042. }
  1043. }
  1044. }
  1045. }
  1046. if (ret)
  1047. dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
  1048. __func__, dev_id);
  1049. pm_runtime_mark_last_busy(swrm->dev);
  1050. pm_runtime_put_autosuspend(swrm->dev);
  1051. return ret;
  1052. }
  1053. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1054. {
  1055. int ret = 0;
  1056. u32 val;
  1057. u8 row_ctrl = SWR_MAX_ROW;
  1058. u8 col_ctrl = SWR_MIN_COL;
  1059. u8 ssp_period = 1;
  1060. u8 retry_cmd_num = 3;
  1061. u32 reg[SWRM_MAX_INIT_REG];
  1062. u32 value[SWRM_MAX_INIT_REG];
  1063. int len = 0;
  1064. /* Clear Rows and Cols */
  1065. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1066. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1067. (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1068. reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
  1069. value[len++] = val;
  1070. /* Set Auto enumeration flag */
  1071. reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
  1072. value[len++] = 1;
  1073. /* Mask soundwire interrupts */
  1074. reg[len] = SWRM_INTERRUPT_MASK_ADDR;
  1075. value[len++] = 0x1FFFD;
  1076. /* Configure No pings */
  1077. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1078. val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
  1079. val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
  1080. reg[len] = SWRM_MCP_CFG_ADDR;
  1081. value[len++] = val;
  1082. /* Configure number of retries of a read/write cmd */
  1083. val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
  1084. reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
  1085. value[len++] = val;
  1086. /* Set IRQ to PULSE */
  1087. reg[len] = SWRM_COMP_CFG_ADDR;
  1088. value[len++] = 0x02;
  1089. reg[len] = SWRM_COMP_CFG_ADDR;
  1090. value[len++] = 0x03;
  1091. reg[len] = SWRM_INTERRUPT_CLEAR;
  1092. value[len++] = 0x08;
  1093. swr_master_bulk_write(swrm, reg, value, len);
  1094. return ret;
  1095. }
  1096. static int swrm_probe(struct platform_device *pdev)
  1097. {
  1098. struct swr_mstr_ctrl *swrm;
  1099. struct swr_ctrl_platform_data *pdata;
  1100. u32 i, num_ports, port_num, port_type, ch_mask;
  1101. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  1102. int ret = 0;
  1103. /* Allocate soundwire master driver structure */
  1104. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  1105. GFP_KERNEL);
  1106. if (!swrm) {
  1107. ret = -ENOMEM;
  1108. goto err_memory_fail;
  1109. }
  1110. swrm->dev = &pdev->dev;
  1111. platform_set_drvdata(pdev, swrm);
  1112. swr_set_ctrl_data(&swrm->master, swrm);
  1113. pdata = dev_get_platdata(&pdev->dev);
  1114. if (!pdata) {
  1115. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  1116. __func__);
  1117. ret = -EINVAL;
  1118. goto err_pdata_fail;
  1119. }
  1120. swrm->handle = (void *)pdata->handle;
  1121. if (!swrm->handle) {
  1122. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  1123. __func__);
  1124. ret = -EINVAL;
  1125. goto err_pdata_fail;
  1126. }
  1127. if (!(of_property_read_u32(pdev->dev.of_node, "swrm-io-base", NULL)))
  1128. swrm->swrm_base_reg = of_property_read_u32(pdev->dev.of_node,
  1129. "swrm-io-base", &swrm->swrm_base_reg);
  1130. if (!swrm->swrm_base_reg) {
  1131. swrm->read = pdata->read;
  1132. if (!swrm->read) {
  1133. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  1134. __func__);
  1135. ret = -EINVAL;
  1136. goto err_pdata_fail;
  1137. }
  1138. swrm->write = pdata->write;
  1139. if (!swrm->write) {
  1140. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  1141. __func__);
  1142. ret = -EINVAL;
  1143. goto err_pdata_fail;
  1144. }
  1145. swrm->bulk_write = pdata->bulk_write;
  1146. if (!swrm->bulk_write) {
  1147. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  1148. __func__);
  1149. ret = -EINVAL;
  1150. goto err_pdata_fail;
  1151. }
  1152. } else {
  1153. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  1154. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  1155. }
  1156. swrm->clk = pdata->clk;
  1157. if (!swrm->clk) {
  1158. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  1159. __func__);
  1160. ret = -EINVAL;
  1161. goto err_pdata_fail;
  1162. }
  1163. /* Parse soundwire port mapping */
  1164. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  1165. &num_ports);
  1166. if (ret) {
  1167. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  1168. goto err_pdata_fail;
  1169. }
  1170. swrm->num_ports = num_ports;
  1171. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  1172. &map_size)) {
  1173. dev_err(swrm->dev, "missing port mapping\n");
  1174. goto err_pdata_fail;
  1175. }
  1176. map_length = map_size / (3 * sizeof(u32));
  1177. if (num_ports > SWR_MSTR_PORT_LEN) {
  1178. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  1179. __func__);
  1180. ret = -EINVAL;
  1181. goto err_pdata_fail;
  1182. }
  1183. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  1184. if (!temp) {
  1185. ret = -ENOMEM;
  1186. goto err_pdata_fail;
  1187. }
  1188. ret = of_property_read_u32_array(pdev->dev.of_node,
  1189. "qcom,swr-port-mapping", temp, 3 * map_length);
  1190. if (ret) {
  1191. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  1192. __func__);
  1193. goto err_pdata_fail;
  1194. }
  1195. for (i = 0; i < map_length; i++) {
  1196. port_num = temp[3 * i];
  1197. port_type = temp[3 * i + 1];
  1198. ch_mask = temp[3 * i + 2];
  1199. if (port_num != old_port_num)
  1200. ch_iter = 0;
  1201. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  1202. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  1203. old_port_num = port_num;
  1204. }
  1205. devm_kfree(&pdev->dev, temp);
  1206. swrm->reg_irq = pdata->reg_irq;
  1207. swrm->master.read = swrm_read;
  1208. swrm->master.write = swrm_write;
  1209. swrm->master.bulk_write = swrm_bulk_write;
  1210. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  1211. swrm->master.connect_port = swrm_connect_port;
  1212. swrm->master.disconnect_port = swrm_disconnect_port;
  1213. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  1214. swrm->master.remove_from_group = swrm_remove_from_group;
  1215. swrm->master.dev.parent = &pdev->dev;
  1216. swrm->master.dev.of_node = pdev->dev.of_node;
  1217. swrm->master.num_port = 0;
  1218. swrm->rcmd_id = 0;
  1219. swrm->wcmd_id = 0;
  1220. swrm->slave_status = 0;
  1221. swrm->num_rx_chs = 0;
  1222. swrm->clk_ref_count = 0;
  1223. swrm->state = SWR_MSTR_RESUME;
  1224. init_completion(&swrm->reset);
  1225. init_completion(&swrm->broadcast);
  1226. mutex_init(&swrm->mlock);
  1227. mutex_init(&swrm->reslock);
  1228. mutex_init(&swrm->force_down_lock);
  1229. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
  1230. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  1231. if (swrm->reg_irq) {
  1232. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  1233. SWR_IRQ_REGISTER);
  1234. if (ret) {
  1235. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  1236. __func__, ret);
  1237. goto err_irq_fail;
  1238. }
  1239. } else {
  1240. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  1241. if (swrm->irq < 0) {
  1242. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  1243. __func__, swrm->irq);
  1244. goto err_pdata_fail;
  1245. }
  1246. ret = request_threaded_irq(swrm->irq, NULL,
  1247. swr_mstr_interrupt,
  1248. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1249. "swr_master_irq", swrm);
  1250. if (ret) {
  1251. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1252. __func__, ret);
  1253. goto err_irq_fail;
  1254. }
  1255. }
  1256. ret = swr_register_master(&swrm->master);
  1257. if (ret) {
  1258. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  1259. goto err_mstr_fail;
  1260. }
  1261. /* Add devices registered with board-info as the
  1262. * controller will be up now
  1263. */
  1264. swr_master_add_boarddevices(&swrm->master);
  1265. mutex_lock(&swrm->mlock);
  1266. swrm_clk_request(swrm, true);
  1267. ret = swrm_master_init(swrm);
  1268. if (ret < 0) {
  1269. dev_err(&pdev->dev,
  1270. "%s: Error in master Initialization , err %d\n",
  1271. __func__, ret);
  1272. mutex_unlock(&swrm->mlock);
  1273. goto err_mstr_fail;
  1274. }
  1275. swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  1276. mutex_unlock(&swrm->mlock);
  1277. if (pdev->dev.of_node)
  1278. of_register_swr_devices(&swrm->master);
  1279. dbgswrm = swrm;
  1280. debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  1281. if (!IS_ERR(debugfs_swrm_dent)) {
  1282. debugfs_peek = debugfs_create_file("swrm_peek",
  1283. S_IFREG | 0444, debugfs_swrm_dent,
  1284. (void *) "swrm_peek", &swrm_debug_ops);
  1285. debugfs_poke = debugfs_create_file("swrm_poke",
  1286. S_IFREG | 0444, debugfs_swrm_dent,
  1287. (void *) "swrm_poke", &swrm_debug_ops);
  1288. debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  1289. S_IFREG | 0444, debugfs_swrm_dent,
  1290. (void *) "swrm_reg_dump",
  1291. &swrm_debug_ops);
  1292. }
  1293. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1294. pm_runtime_use_autosuspend(&pdev->dev);
  1295. pm_runtime_set_active(&pdev->dev);
  1296. pm_runtime_enable(&pdev->dev);
  1297. pm_runtime_mark_last_busy(&pdev->dev);
  1298. return 0;
  1299. err_mstr_fail:
  1300. if (swrm->reg_irq)
  1301. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1302. swrm, SWR_IRQ_FREE);
  1303. else if (swrm->irq)
  1304. free_irq(swrm->irq, swrm);
  1305. err_irq_fail:
  1306. mutex_destroy(&swrm->mlock);
  1307. mutex_destroy(&swrm->reslock);
  1308. mutex_destroy(&swrm->force_down_lock);
  1309. err_pdata_fail:
  1310. err_memory_fail:
  1311. return ret;
  1312. }
  1313. static int swrm_remove(struct platform_device *pdev)
  1314. {
  1315. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1316. if (swrm->reg_irq)
  1317. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1318. swrm, SWR_IRQ_FREE);
  1319. else if (swrm->irq)
  1320. free_irq(swrm->irq, swrm);
  1321. pm_runtime_disable(&pdev->dev);
  1322. pm_runtime_set_suspended(&pdev->dev);
  1323. swr_unregister_master(&swrm->master);
  1324. mutex_destroy(&swrm->mlock);
  1325. mutex_destroy(&swrm->reslock);
  1326. mutex_destroy(&swrm->force_down_lock);
  1327. devm_kfree(&pdev->dev, swrm);
  1328. return 0;
  1329. }
  1330. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  1331. {
  1332. u32 val;
  1333. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  1334. swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
  1335. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1336. val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
  1337. swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
  1338. swrm->state = SWR_MSTR_PAUSE;
  1339. return 0;
  1340. }
  1341. #ifdef CONFIG_PM
  1342. static int swrm_runtime_resume(struct device *dev)
  1343. {
  1344. struct platform_device *pdev = to_platform_device(dev);
  1345. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1346. int ret = 0;
  1347. struct swr_master *mstr = &swrm->master;
  1348. struct swr_device *swr_dev;
  1349. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  1350. __func__, swrm->state);
  1351. mutex_lock(&swrm->reslock);
  1352. if ((swrm->state == SWR_MSTR_PAUSE) ||
  1353. (swrm->state == SWR_MSTR_DOWN)) {
  1354. if (swrm->state == SWR_MSTR_DOWN) {
  1355. if (swrm_clk_request(swrm, true))
  1356. goto exit;
  1357. }
  1358. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1359. ret = swr_device_up(swr_dev);
  1360. if (ret) {
  1361. dev_err(dev,
  1362. "%s: failed to wakeup swr dev %d\n",
  1363. __func__, swr_dev->dev_num);
  1364. swrm_clk_request(swrm, false);
  1365. goto exit;
  1366. }
  1367. }
  1368. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1369. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1370. swrm_master_init(swrm);
  1371. }
  1372. exit:
  1373. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1374. mutex_unlock(&swrm->reslock);
  1375. return ret;
  1376. }
  1377. static int swrm_runtime_suspend(struct device *dev)
  1378. {
  1379. struct platform_device *pdev = to_platform_device(dev);
  1380. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1381. int ret = 0;
  1382. struct swr_master *mstr = &swrm->master;
  1383. struct swr_device *swr_dev;
  1384. int current_state = 0;
  1385. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  1386. __func__, swrm->state);
  1387. mutex_lock(&swrm->reslock);
  1388. mutex_lock(&swrm->force_down_lock);
  1389. current_state = swrm->state;
  1390. mutex_unlock(&swrm->force_down_lock);
  1391. if ((current_state == SWR_MSTR_RESUME) ||
  1392. (current_state == SWR_MSTR_UP) ||
  1393. (current_state == SWR_MSTR_SSR)) {
  1394. if ((current_state != SWR_MSTR_SSR) &&
  1395. swrm_is_port_en(&swrm->master)) {
  1396. dev_dbg(dev, "%s ports are enabled\n", __func__);
  1397. ret = -EBUSY;
  1398. goto exit;
  1399. }
  1400. swrm_clk_pause(swrm);
  1401. swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
  1402. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1403. ret = swr_device_down(swr_dev);
  1404. if (ret) {
  1405. dev_err(dev,
  1406. "%s: failed to shutdown swr dev %d\n",
  1407. __func__, swr_dev->dev_num);
  1408. goto exit;
  1409. }
  1410. }
  1411. swrm_clk_request(swrm, false);
  1412. }
  1413. exit:
  1414. mutex_unlock(&swrm->reslock);
  1415. return ret;
  1416. }
  1417. #endif /* CONFIG_PM */
  1418. static int swrm_device_down(struct device *dev)
  1419. {
  1420. struct platform_device *pdev = to_platform_device(dev);
  1421. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1422. int ret = 0;
  1423. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  1424. mutex_lock(&swrm->force_down_lock);
  1425. swrm->state = SWR_MSTR_SSR;
  1426. mutex_unlock(&swrm->force_down_lock);
  1427. /* Use pm runtime function to tear down */
  1428. ret = pm_runtime_put_sync_suspend(dev);
  1429. pm_runtime_get_noresume(dev);
  1430. return ret;
  1431. }
  1432. /**
  1433. * swrm_wcd_notify - parent device can notify to soundwire master through
  1434. * this function
  1435. * @pdev: pointer to platform device structure
  1436. * @id: command id from parent to the soundwire master
  1437. * @data: data from parent device to soundwire master
  1438. */
  1439. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  1440. {
  1441. struct swr_mstr_ctrl *swrm;
  1442. int ret = 0;
  1443. struct swr_master *mstr;
  1444. struct swr_device *swr_dev;
  1445. if (!pdev) {
  1446. pr_err("%s: pdev is NULL\n", __func__);
  1447. return -EINVAL;
  1448. }
  1449. swrm = platform_get_drvdata(pdev);
  1450. if (!swrm) {
  1451. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  1452. return -EINVAL;
  1453. }
  1454. mstr = &swrm->master;
  1455. switch (id) {
  1456. case SWR_DEVICE_DOWN:
  1457. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  1458. mutex_lock(&swrm->mlock);
  1459. if ((swrm->state == SWR_MSTR_PAUSE) ||
  1460. (swrm->state == SWR_MSTR_DOWN))
  1461. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  1462. __func__, swrm->state);
  1463. else
  1464. swrm_device_down(&pdev->dev);
  1465. mutex_unlock(&swrm->mlock);
  1466. break;
  1467. case SWR_DEVICE_UP:
  1468. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  1469. mutex_lock(&swrm->mlock);
  1470. mutex_lock(&swrm->reslock);
  1471. if ((swrm->state == SWR_MSTR_RESUME) ||
  1472. (swrm->state == SWR_MSTR_UP)) {
  1473. dev_dbg(swrm->dev, "%s: SWR master is already UP: %d\n",
  1474. __func__, swrm->state);
  1475. } else {
  1476. pm_runtime_mark_last_busy(&pdev->dev);
  1477. mutex_unlock(&swrm->reslock);
  1478. pm_runtime_get_sync(&pdev->dev);
  1479. mutex_lock(&swrm->reslock);
  1480. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1481. ret = swr_reset_device(swr_dev);
  1482. if (ret) {
  1483. dev_err(swrm->dev,
  1484. "%s: failed to reset swr device %d\n",
  1485. __func__, swr_dev->dev_num);
  1486. swrm_clk_request(swrm, false);
  1487. }
  1488. }
  1489. pm_runtime_mark_last_busy(&pdev->dev);
  1490. pm_runtime_put_autosuspend(&pdev->dev);
  1491. }
  1492. mutex_unlock(&swrm->reslock);
  1493. mutex_unlock(&swrm->mlock);
  1494. break;
  1495. case SWR_SET_NUM_RX_CH:
  1496. if (!data) {
  1497. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1498. ret = -EINVAL;
  1499. } else {
  1500. mutex_lock(&swrm->mlock);
  1501. swrm->num_rx_chs = *(int *)data;
  1502. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  1503. list_for_each_entry(swr_dev, &mstr->devices,
  1504. dev_list) {
  1505. ret = swr_set_device_group(swr_dev,
  1506. SWR_BROADCAST);
  1507. if (ret)
  1508. dev_err(swrm->dev,
  1509. "%s: set num ch failed\n",
  1510. __func__);
  1511. }
  1512. } else {
  1513. list_for_each_entry(swr_dev, &mstr->devices,
  1514. dev_list) {
  1515. ret = swr_set_device_group(swr_dev,
  1516. SWR_GROUP_NONE);
  1517. if (ret)
  1518. dev_err(swrm->dev,
  1519. "%s: set num ch failed\n",
  1520. __func__);
  1521. }
  1522. }
  1523. mutex_unlock(&swrm->mlock);
  1524. }
  1525. break;
  1526. default:
  1527. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  1528. __func__, id);
  1529. break;
  1530. }
  1531. return ret;
  1532. }
  1533. EXPORT_SYMBOL(swrm_wcd_notify);
  1534. #ifdef CONFIG_PM_SLEEP
  1535. static int swrm_suspend(struct device *dev)
  1536. {
  1537. int ret = -EBUSY;
  1538. struct platform_device *pdev = to_platform_device(dev);
  1539. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1540. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  1541. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  1542. ret = swrm_runtime_suspend(dev);
  1543. if (!ret) {
  1544. /*
  1545. * Synchronize runtime-pm and system-pm states:
  1546. * At this point, we are already suspended. If
  1547. * runtime-pm still thinks its active, then
  1548. * make sure its status is in sync with HW
  1549. * status. The three below calls let the
  1550. * runtime-pm know that we are suspended
  1551. * already without re-invoking the suspend
  1552. * callback
  1553. */
  1554. pm_runtime_disable(dev);
  1555. pm_runtime_set_suspended(dev);
  1556. pm_runtime_enable(dev);
  1557. }
  1558. }
  1559. if (ret == -EBUSY) {
  1560. /*
  1561. * There is a possibility that some audio stream is active
  1562. * during suspend. We dont want to return suspend failure in
  1563. * that case so that display and relevant components can still
  1564. * go to suspend.
  1565. * If there is some other error, then it should be passed-on
  1566. * to system level suspend
  1567. */
  1568. ret = 0;
  1569. }
  1570. return ret;
  1571. }
  1572. static int swrm_resume(struct device *dev)
  1573. {
  1574. int ret = 0;
  1575. struct platform_device *pdev = to_platform_device(dev);
  1576. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1577. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  1578. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  1579. ret = swrm_runtime_resume(dev);
  1580. if (!ret) {
  1581. pm_runtime_mark_last_busy(dev);
  1582. pm_request_autosuspend(dev);
  1583. }
  1584. }
  1585. return ret;
  1586. }
  1587. #endif /* CONFIG_PM_SLEEP */
  1588. static const struct dev_pm_ops swrm_dev_pm_ops = {
  1589. SET_SYSTEM_SLEEP_PM_OPS(
  1590. swrm_suspend,
  1591. swrm_resume
  1592. )
  1593. SET_RUNTIME_PM_OPS(
  1594. swrm_runtime_suspend,
  1595. swrm_runtime_resume,
  1596. NULL
  1597. )
  1598. };
  1599. static const struct of_device_id swrm_dt_match[] = {
  1600. {
  1601. .compatible = "qcom,swr-mstr",
  1602. },
  1603. {}
  1604. };
  1605. static struct platform_driver swr_mstr_driver = {
  1606. .probe = swrm_probe,
  1607. .remove = swrm_remove,
  1608. .driver = {
  1609. .name = SWR_WCD_NAME,
  1610. .owner = THIS_MODULE,
  1611. .pm = &swrm_dev_pm_ops,
  1612. .of_match_table = swrm_dt_match,
  1613. },
  1614. };
  1615. static int __init swrm_init(void)
  1616. {
  1617. return platform_driver_register(&swr_mstr_driver);
  1618. }
  1619. module_init(swrm_init);
  1620. static void __exit swrm_exit(void)
  1621. {
  1622. platform_driver_unregister(&swr_mstr_driver);
  1623. }
  1624. module_exit(swrm_exit);
  1625. MODULE_LICENSE("GPL v2");
  1626. MODULE_DESCRIPTION("SoundWire Master Controller");
  1627. MODULE_ALIAS("platform:swr-mstr");