swr-wcd-ctrl.c 48 KB

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