swr-wcd-ctrl.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. // SPDX-License-Identifier: GPL-2.0
  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. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  583. SWRS_SCP_FRAME_CTRL_BANK(bank));
  584. }
  585. static struct swr_port_info *swrm_get_port(struct swr_master *master,
  586. u8 port_id)
  587. {
  588. int i;
  589. struct swr_port_info *port = NULL;
  590. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  591. port = &master->port[i];
  592. if (port->slave_port_id == port_id) {
  593. dev_dbg(&master->dev, "%s: port_id: %d, index: %d\n",
  594. __func__, port_id, i);
  595. return port;
  596. }
  597. }
  598. return NULL;
  599. }
  600. static struct swr_port_info *swrm_get_avail_port(struct swr_master *master)
  601. {
  602. int i;
  603. struct swr_port_info *port = NULL;
  604. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  605. port = &master->port[i];
  606. if (port->port_en)
  607. continue;
  608. dev_dbg(&master->dev, "%s: port_id: %d, index: %d\n",
  609. __func__, port->slave_port_id, i);
  610. return port;
  611. }
  612. return NULL;
  613. }
  614. static struct swr_port_info *swrm_get_enabled_port(struct swr_master *master,
  615. u8 port_id)
  616. {
  617. int i;
  618. struct swr_port_info *port = NULL;
  619. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  620. port = &master->port[i];
  621. if ((port->slave_port_id == port_id) && (port->port_en == true))
  622. break;
  623. }
  624. if (i == SWR_MSTR_PORT_LEN)
  625. port = NULL;
  626. return port;
  627. }
  628. static bool swrm_remove_from_group(struct swr_master *master)
  629. {
  630. struct swr_device *swr_dev;
  631. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  632. bool is_removed = false;
  633. if (!swrm)
  634. goto end;
  635. mutex_lock(&swrm->mlock);
  636. if ((swrm->num_rx_chs > 1) &&
  637. (swrm->num_rx_chs == swrm->num_cfg_devs)) {
  638. list_for_each_entry(swr_dev, &master->devices,
  639. dev_list) {
  640. swr_dev->group_id = SWR_GROUP_NONE;
  641. master->gr_sid = 0;
  642. }
  643. is_removed = true;
  644. }
  645. mutex_unlock(&swrm->mlock);
  646. end:
  647. return is_removed;
  648. }
  649. static void swrm_cleanup_disabled_data_ports(struct swr_master *master,
  650. u8 bank)
  651. {
  652. u32 value;
  653. struct swr_port_info *port;
  654. int i;
  655. int port_type;
  656. struct swrm_mports *mport, *mport_next = NULL;
  657. int port_disable_cnt = 0;
  658. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  659. if (!swrm) {
  660. pr_err("%s: swrm is null\n", __func__);
  661. return;
  662. }
  663. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  664. master->num_port);
  665. mport = list_first_entry_or_null(&swrm->mport_list,
  666. struct swrm_mports,
  667. list);
  668. if (!mport) {
  669. dev_err(swrm->dev, "%s: list is empty\n", __func__);
  670. return;
  671. }
  672. for (i = 0; i < master->num_port; i++) {
  673. port = swrm_get_port(master, mstr_ports[mport->id]);
  674. if (!port || port->ch_en)
  675. goto inc_loop;
  676. port_disable_cnt++;
  677. port_type = mstr_port_type[mport->id];
  678. value = ((port->ch_en)
  679. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  680. value |= ((port->offset2)
  681. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  682. value |= ((port->offset1)
  683. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  684. value |= port->sinterval;
  685. swrm->write(swrm->handle,
  686. SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank),
  687. value);
  688. swrm_cmd_fifo_wr_cmd(swrm, 0x00, port->dev_num, 0x00,
  689. SWRS_DP_CHANNEL_ENABLE_BANK(port_type, bank));
  690. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  691. __func__, mport->id,
  692. (SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank)), value);
  693. inc_loop:
  694. mport_next = list_next_entry(mport, list);
  695. if (port && !port->ch_en) {
  696. list_del(&mport->list);
  697. kfree(mport);
  698. }
  699. if (!mport_next) {
  700. dev_err(swrm->dev, "%s: end of list\n", __func__);
  701. break;
  702. }
  703. mport = mport_next;
  704. }
  705. master->num_port -= port_disable_cnt;
  706. dev_dbg(swrm->dev, "%s:disable ports: %d, active ports (rem): %d\n",
  707. __func__, port_disable_cnt, master->num_port);
  708. }
  709. static int swrm_slvdev_datapath_control(struct swr_master *master,
  710. bool enable)
  711. {
  712. u8 bank;
  713. u32 value, n_col;
  714. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  715. int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
  716. SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
  717. SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
  718. u8 inactive_bank;
  719. if (!swrm) {
  720. pr_err("%s: swrm is null\n", __func__);
  721. return 0;
  722. }
  723. bank = get_inactive_bank_num(swrm);
  724. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
  725. __func__, enable, swrm->num_cfg_devs);
  726. if (enable) {
  727. /* set Row = 48 and col = 16 */
  728. n_col = SWR_MAX_COL;
  729. } else {
  730. /*
  731. * Do not change to 48x2 if number of channels configured
  732. * as stereo and if disable datapath is called for the
  733. * first slave device
  734. */
  735. if (swrm->num_cfg_devs > 0)
  736. n_col = SWR_MAX_COL;
  737. else
  738. n_col = SWR_MIN_COL;
  739. /*
  740. * All ports are already disabled, no need to perform
  741. * bank-switch and copy operation. This case can arise
  742. * when speaker channels are enabled in stereo mode with
  743. * BROADCAST and disabled in GROUP_NONE
  744. */
  745. if (master->num_port == 0)
  746. return 0;
  747. }
  748. value = swrm->read(swrm->handle, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
  749. value &= (~mask);
  750. value |= ((0 << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  751. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  752. (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  753. swrm->write(swrm->handle, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  754. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  755. SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  756. enable_bank_switch(swrm, bank, SWR_MAX_ROW, n_col);
  757. inactive_bank = bank ? 0 : 1;
  758. if (enable)
  759. swrm_copy_data_port_config(master, inactive_bank);
  760. else
  761. swrm_cleanup_disabled_data_ports(master, inactive_bank);
  762. if (!swrm_is_port_en(master)) {
  763. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  764. __func__);
  765. pm_runtime_mark_last_busy(&swrm->pdev->dev);
  766. pm_runtime_put_autosuspend(&swrm->pdev->dev);
  767. }
  768. return 0;
  769. }
  770. static void swrm_apply_port_config(struct swr_master *master)
  771. {
  772. u8 bank;
  773. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  774. if (!swrm) {
  775. pr_err("%s: Invalid handle to swr controller\n",
  776. __func__);
  777. return;
  778. }
  779. bank = get_inactive_bank_num(swrm);
  780. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  781. __func__, bank, master->num_port);
  782. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
  783. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  784. swrm_copy_data_port_config(master, bank);
  785. }
  786. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  787. {
  788. u32 value;
  789. struct swr_port_info *port;
  790. int i;
  791. int port_type;
  792. struct swrm_mports *mport;
  793. u32 reg[SWRM_MAX_PORT_REG];
  794. u32 val[SWRM_MAX_PORT_REG];
  795. int len = 0;
  796. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  797. if (!swrm) {
  798. pr_err("%s: swrm is null\n", __func__);
  799. return;
  800. }
  801. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  802. master->num_port);
  803. mport = list_first_entry_or_null(&swrm->mport_list,
  804. struct swrm_mports,
  805. list);
  806. if (!mport) {
  807. dev_err(swrm->dev, "%s: list is empty\n", __func__);
  808. return;
  809. }
  810. for (i = 0; i < master->num_port; i++) {
  811. port = swrm_get_enabled_port(master, mstr_ports[mport->id]);
  812. if (!port)
  813. continue;
  814. port_type = mstr_port_type[mport->id];
  815. if (!port->dev_num || (port->dev_num > master->num_dev)) {
  816. dev_dbg(swrm->dev, "%s: invalid device id = %d\n",
  817. __func__, port->dev_num);
  818. continue;
  819. }
  820. value = ((port->ch_en)
  821. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  822. value |= ((port->offset2)
  823. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  824. value |= ((port->offset1)
  825. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  826. value |= port->sinterval;
  827. reg[len] = SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank);
  828. val[len++] = value;
  829. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  830. __func__, mport->id,
  831. (SWRM_DP_PORT_CTRL_BANK((mport->id+1), bank)), value);
  832. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  833. val[len++] = SWR_REG_VAL_PACK(port->ch_en, port->dev_num, 0x00,
  834. SWRS_DP_CHANNEL_ENABLE_BANK(port_type, bank));
  835. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  836. val[len++] = SWR_REG_VAL_PACK(port->sinterval,
  837. port->dev_num, 0x00,
  838. SWRS_DP_SAMPLE_CONTROL_1_BANK(port_type, bank));
  839. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  840. val[len++] = SWR_REG_VAL_PACK(port->offset1,
  841. port->dev_num, 0x00,
  842. SWRS_DP_OFFSET_CONTROL_1_BANK(port_type, bank));
  843. if (port_type != 0) {
  844. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  845. val[len++] = SWR_REG_VAL_PACK(port->offset2,
  846. port->dev_num, 0x00,
  847. SWRS_DP_OFFSET_CONTROL_2_BANK(port_type,
  848. bank));
  849. }
  850. mport = list_next_entry(mport, list);
  851. if (!mport) {
  852. dev_err(swrm->dev, "%s: end of list\n", __func__);
  853. break;
  854. }
  855. }
  856. swrm->bulk_write(swrm->handle, reg, val, len);
  857. }
  858. static int swrm_connect_port(struct swr_master *master,
  859. struct swr_params *portinfo)
  860. {
  861. int i;
  862. struct swr_port_info *port;
  863. int ret = 0;
  864. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  865. struct swrm_mports *mport;
  866. struct list_head *ptr, *next;
  867. dev_dbg(&master->dev, "%s: enter\n", __func__);
  868. if (!portinfo)
  869. return -EINVAL;
  870. if (!swrm) {
  871. dev_err(&master->dev,
  872. "%s: Invalid handle to swr controller\n",
  873. __func__);
  874. return -EINVAL;
  875. }
  876. mutex_lock(&swrm->mlock);
  877. if (!swrm_is_port_en(master))
  878. pm_runtime_get_sync(&swrm->pdev->dev);
  879. for (i = 0; i < portinfo->num_port; i++) {
  880. mport = kzalloc(sizeof(struct swrm_mports), GFP_KERNEL);
  881. if (!mport) {
  882. ret = -ENOMEM;
  883. goto mem_fail;
  884. }
  885. ret = swrm_get_master_port(&mport->id,
  886. portinfo->port_id[i]);
  887. if (ret < 0) {
  888. dev_err(&master->dev,
  889. "%s: mstr portid for slv port %d not found\n",
  890. __func__, portinfo->port_id[i]);
  891. goto port_fail;
  892. }
  893. port = swrm_get_avail_port(master);
  894. if (!port) {
  895. dev_err(&master->dev,
  896. "%s: avail ports not found!\n", __func__);
  897. goto port_fail;
  898. }
  899. list_add(&mport->list, &swrm->mport_list);
  900. port->dev_num = portinfo->dev_num;
  901. port->slave_port_id = portinfo->port_id[i];
  902. port->num_ch = portinfo->num_ch[i];
  903. port->ch_rate = portinfo->ch_rate[i];
  904. port->ch_en = portinfo->ch_en[i];
  905. port->port_en = true;
  906. dev_dbg(&master->dev,
  907. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  908. __func__, mport->id, port->slave_port_id, port->ch_rate,
  909. port->num_ch);
  910. }
  911. master->num_port += portinfo->num_port;
  912. if (master->num_port >= SWR_MSTR_PORT_LEN)
  913. master->num_port = SWR_MSTR_PORT_LEN;
  914. swrm_get_port_config(master);
  915. swr_port_response(master, portinfo->tid);
  916. swrm->num_cfg_devs += 1;
  917. dev_dbg(&master->dev, "%s: cfg_devs: %d, rx_chs: %d\n",
  918. __func__, swrm->num_cfg_devs, swrm->num_rx_chs);
  919. if (swrm->num_rx_chs > 1) {
  920. if (swrm->num_rx_chs == swrm->num_cfg_devs)
  921. swrm_apply_port_config(master);
  922. } else {
  923. swrm_apply_port_config(master);
  924. }
  925. mutex_unlock(&swrm->mlock);
  926. return 0;
  927. port_fail:
  928. kfree(mport);
  929. mem_fail:
  930. list_for_each_safe(ptr, next, &swrm->mport_list) {
  931. mport = list_entry(ptr, struct swrm_mports, list);
  932. for (i = 0; i < portinfo->num_port; i++) {
  933. if (portinfo->port_id[i] == mstr_ports[mport->id]) {
  934. port = swrm_get_port(master,
  935. portinfo->port_id[i]);
  936. if (port)
  937. port->ch_en = false;
  938. list_del(&mport->list);
  939. kfree(mport);
  940. break;
  941. }
  942. }
  943. }
  944. mutex_unlock(&swrm->mlock);
  945. return ret;
  946. }
  947. static int swrm_disconnect_port(struct swr_master *master,
  948. struct swr_params *portinfo)
  949. {
  950. int i;
  951. struct swr_port_info *port;
  952. u8 bank;
  953. u32 value;
  954. int ret = 0;
  955. u8 mport_id = 0;
  956. int port_type = 0;
  957. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  958. if (!swrm) {
  959. dev_err(&master->dev,
  960. "%s: Invalid handle to swr controller\n",
  961. __func__);
  962. return -EINVAL;
  963. }
  964. if (!portinfo) {
  965. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  966. return -EINVAL;
  967. }
  968. mutex_lock(&swrm->mlock);
  969. bank = get_inactive_bank_num(swrm);
  970. for (i = 0; i < portinfo->num_port; i++) {
  971. ret = swrm_get_master_port(&mport_id,
  972. portinfo->port_id[i]);
  973. if (ret < 0) {
  974. dev_err(&master->dev,
  975. "%s: mstr portid for slv port %d not found\n",
  976. __func__, portinfo->port_id[i]);
  977. mutex_unlock(&swrm->mlock);
  978. return -EINVAL;
  979. }
  980. port = swrm_get_enabled_port(master, portinfo->port_id[i]);
  981. if (!port) {
  982. dev_dbg(&master->dev, "%s: port %d already disabled\n",
  983. __func__, portinfo->port_id[i]);
  984. continue;
  985. }
  986. port_type = mstr_port_type[mport_id];
  987. port->dev_num = portinfo->dev_num;
  988. port->port_en = false;
  989. port->ch_en = 0;
  990. value = port->ch_en << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT;
  991. value |= (port->offset2 << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  992. value |= (port->offset1 << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  993. value |= port->sinterval;
  994. swrm->write(swrm->handle,
  995. SWRM_DP_PORT_CTRL_BANK((mport_id+1), bank),
  996. value);
  997. swrm_cmd_fifo_wr_cmd(swrm, 0x00, port->dev_num, 0x00,
  998. SWRS_DP_CHANNEL_ENABLE_BANK(port_type, bank));
  999. }
  1000. swr_port_response(master, portinfo->tid);
  1001. swrm->num_cfg_devs -= 1;
  1002. dev_dbg(&master->dev, "%s: cfg_devs: %d, rx_chs: %d, active ports: %d\n",
  1003. __func__, swrm->num_cfg_devs, swrm->num_rx_chs,
  1004. master->num_port);
  1005. mutex_unlock(&swrm->mlock);
  1006. return 0;
  1007. }
  1008. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1009. int status, u8 *devnum)
  1010. {
  1011. int i;
  1012. int new_sts = status;
  1013. int ret = SWR_NOT_PRESENT;
  1014. if (status != swrm->slave_status) {
  1015. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1016. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1017. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1018. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1019. *devnum = i;
  1020. break;
  1021. }
  1022. status >>= 2;
  1023. swrm->slave_status >>= 2;
  1024. }
  1025. swrm->slave_status = new_sts;
  1026. }
  1027. return ret;
  1028. }
  1029. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1030. {
  1031. struct swr_mstr_ctrl *swrm = dev;
  1032. u32 value, intr_sts;
  1033. int status, chg_sts, i;
  1034. u8 devnum = 0;
  1035. int ret = IRQ_HANDLED;
  1036. mutex_lock(&swrm->reslock);
  1037. swrm_clk_request(swrm, true);
  1038. mutex_unlock(&swrm->reslock);
  1039. intr_sts = swrm->read(swrm->handle, SWRM_INTERRUPT_STATUS);
  1040. intr_sts &= SWRM_INTERRUPT_STATUS_RMSK;
  1041. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1042. value = intr_sts & (1 << i);
  1043. if (!value)
  1044. continue;
  1045. swrm->write(swrm->handle, SWRM_INTERRUPT_CLEAR, value);
  1046. switch (value) {
  1047. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1048. dev_dbg(swrm->dev, "SWR slave pend irq\n");
  1049. break;
  1050. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1051. dev_dbg(swrm->dev, "SWR new slave attached\n");
  1052. break;
  1053. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1054. status = swrm->read(swrm->handle, SWRM_MCP_SLV_STATUS);
  1055. if (status == swrm->slave_status) {
  1056. dev_dbg(swrm->dev,
  1057. "%s: No change in slave status: %d\n",
  1058. __func__, status);
  1059. break;
  1060. }
  1061. chg_sts = swrm_check_slave_change_status(swrm, status,
  1062. &devnum);
  1063. switch (chg_sts) {
  1064. case SWR_NOT_PRESENT:
  1065. dev_dbg(swrm->dev, "device %d got detached\n",
  1066. devnum);
  1067. break;
  1068. case SWR_ATTACHED_OK:
  1069. dev_dbg(swrm->dev, "device %d got attached\n",
  1070. devnum);
  1071. break;
  1072. case SWR_ALERT:
  1073. dev_dbg(swrm->dev,
  1074. "device %d has pending interrupt\n",
  1075. devnum);
  1076. break;
  1077. }
  1078. break;
  1079. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1080. dev_err_ratelimited(swrm->dev, "SWR bus clash detected\n");
  1081. break;
  1082. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1083. dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
  1084. break;
  1085. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1086. dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
  1087. break;
  1088. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1089. dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
  1090. break;
  1091. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1092. value = swrm->read(swrm->handle, SWRM_CMD_FIFO_STATUS);
  1093. dev_err_ratelimited(swrm->dev,
  1094. "SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1095. value);
  1096. swrm->write(swrm->handle, SWRM_CMD_FIFO_CMD, 0x1);
  1097. break;
  1098. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1099. dev_dbg(swrm->dev, "SWR Port collision detected\n");
  1100. break;
  1101. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  1102. dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
  1103. break;
  1104. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  1105. complete(&swrm->broadcast);
  1106. dev_dbg(swrm->dev, "SWR cmd id finished\n");
  1107. break;
  1108. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
  1109. break;
  1110. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  1111. break;
  1112. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  1113. break;
  1114. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  1115. complete(&swrm->reset);
  1116. break;
  1117. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  1118. break;
  1119. default:
  1120. dev_err_ratelimited(swrm->dev, "SWR unknown interrupt\n");
  1121. ret = IRQ_NONE;
  1122. break;
  1123. }
  1124. }
  1125. mutex_lock(&swrm->reslock);
  1126. swrm_clk_request(swrm, false);
  1127. mutex_unlock(&swrm->reslock);
  1128. return ret;
  1129. }
  1130. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  1131. {
  1132. u32 val;
  1133. swrm->slave_status = swrm->read(swrm->handle, SWRM_MCP_SLV_STATUS);
  1134. val = (swrm->slave_status >> (devnum * 2));
  1135. val &= SWRM_MCP_SLV_STATUS_MASK;
  1136. return val;
  1137. }
  1138. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1139. u8 *dev_num)
  1140. {
  1141. int i;
  1142. u64 id = 0;
  1143. int ret = -EINVAL;
  1144. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1145. struct swr_device *swr_dev;
  1146. u32 num_dev = 0;
  1147. if (!swrm) {
  1148. pr_err("%s: Invalid handle to swr controller\n",
  1149. __func__);
  1150. return ret;
  1151. }
  1152. if (swrm->num_dev)
  1153. num_dev = swrm->num_dev;
  1154. else
  1155. num_dev = mstr->num_dev;
  1156. pm_runtime_get_sync(&swrm->pdev->dev);
  1157. for (i = 1; i < (num_dev + 1); i++) {
  1158. id = ((u64)(swrm->read(swrm->handle,
  1159. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1160. id |= swrm->read(swrm->handle,
  1161. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1162. /*
  1163. * As pm_runtime_get_sync() brings all slaves out of reset
  1164. * update logical device number for all slaves.
  1165. */
  1166. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1167. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1168. u32 status = swrm_get_device_status(swrm, i);
  1169. if ((status == 0x01) || (status == 0x02)) {
  1170. swr_dev->dev_num = i;
  1171. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1172. *dev_num = i;
  1173. ret = 0;
  1174. }
  1175. dev_dbg(swrm->dev, "%s: devnum %d is assigned for dev addr %lx\n",
  1176. __func__, i, swr_dev->addr);
  1177. }
  1178. }
  1179. }
  1180. }
  1181. if (ret)
  1182. dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
  1183. __func__, dev_id);
  1184. pm_runtime_mark_last_busy(&swrm->pdev->dev);
  1185. pm_runtime_put_autosuspend(&swrm->pdev->dev);
  1186. return ret;
  1187. }
  1188. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1189. {
  1190. int ret = 0;
  1191. u32 val;
  1192. u8 row_ctrl = SWR_MAX_ROW;
  1193. u8 col_ctrl = SWR_MIN_COL;
  1194. u8 ssp_period = 1;
  1195. u8 retry_cmd_num = 3;
  1196. u32 reg[SWRM_MAX_INIT_REG];
  1197. u32 value[SWRM_MAX_INIT_REG];
  1198. int len = 0;
  1199. /* Clear Rows and Cols */
  1200. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1201. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1202. (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1203. reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
  1204. value[len++] = val;
  1205. /* Set Auto enumeration flag */
  1206. reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
  1207. value[len++] = 1;
  1208. /* Mask soundwire interrupts */
  1209. reg[len] = SWRM_INTERRUPT_MASK_ADDR;
  1210. value[len++] = 0x1FFFD;
  1211. /* Configure No pings */
  1212. val = swrm->read(swrm->handle, SWRM_MCP_CFG_ADDR);
  1213. val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
  1214. val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
  1215. reg[len] = SWRM_MCP_CFG_ADDR;
  1216. value[len++] = val;
  1217. /* Configure number of retries of a read/write cmd */
  1218. val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
  1219. reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
  1220. value[len++] = val;
  1221. /* Set IRQ to PULSE */
  1222. reg[len] = SWRM_COMP_CFG_ADDR;
  1223. value[len++] = 0x02;
  1224. reg[len] = SWRM_COMP_CFG_ADDR;
  1225. value[len++] = 0x03;
  1226. reg[len] = SWRM_INTERRUPT_CLEAR;
  1227. value[len++] = 0x08;
  1228. swrm->bulk_write(swrm->handle, reg, value, len);
  1229. return ret;
  1230. }
  1231. static int swrm_event_notify(struct notifier_block *self,
  1232. unsigned long action, void *data)
  1233. {
  1234. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  1235. event_notifier);
  1236. if (!swrm || !swrm->pdev) {
  1237. pr_err("%s: swrm or pdev is NULL\n", __func__);
  1238. return -EINVAL;
  1239. }
  1240. if (action != MSM_AUD_DC_EVENT) {
  1241. dev_err(&swrm->pdev->dev, "%s: invalid event type: %lu\n", __func__, action);
  1242. return -EINVAL;
  1243. }
  1244. schedule_work(&(swrm->dc_presence_work));
  1245. return 0;
  1246. }
  1247. static void swrm_notify_work_fn(struct work_struct *work)
  1248. {
  1249. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  1250. dc_presence_work);
  1251. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  1252. }
  1253. static int swrm_probe(struct platform_device *pdev)
  1254. {
  1255. struct swr_mstr_ctrl *swrm;
  1256. struct swr_ctrl_platform_data *pdata;
  1257. int ret;
  1258. /* Allocate soundwire master driver structure */
  1259. swrm = kzalloc(sizeof(struct swr_mstr_ctrl), GFP_KERNEL);
  1260. if (!swrm) {
  1261. ret = -ENOMEM;
  1262. goto err_memory_fail;
  1263. }
  1264. swrm->dev = &pdev->dev;
  1265. swrm->pdev = pdev;
  1266. platform_set_drvdata(pdev, swrm);
  1267. swr_set_ctrl_data(&swrm->master, swrm);
  1268. pdata = dev_get_platdata(&pdev->dev);
  1269. if (!pdata) {
  1270. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  1271. __func__);
  1272. ret = -EINVAL;
  1273. goto err_pdata_fail;
  1274. }
  1275. swrm->handle = (void *)pdata->handle;
  1276. if (!swrm->handle) {
  1277. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  1278. __func__);
  1279. ret = -EINVAL;
  1280. goto err_pdata_fail;
  1281. }
  1282. swrm->read = pdata->read;
  1283. if (!swrm->read) {
  1284. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  1285. __func__);
  1286. ret = -EINVAL;
  1287. goto err_pdata_fail;
  1288. }
  1289. swrm->write = pdata->write;
  1290. if (!swrm->write) {
  1291. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  1292. __func__);
  1293. ret = -EINVAL;
  1294. goto err_pdata_fail;
  1295. }
  1296. swrm->bulk_write = pdata->bulk_write;
  1297. if (!swrm->bulk_write) {
  1298. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  1299. __func__);
  1300. ret = -EINVAL;
  1301. goto err_pdata_fail;
  1302. }
  1303. swrm->clk = pdata->clk;
  1304. if (!swrm->clk) {
  1305. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  1306. __func__);
  1307. ret = -EINVAL;
  1308. goto err_pdata_fail;
  1309. }
  1310. swrm->reg_irq = pdata->reg_irq;
  1311. if (!swrm->reg_irq) {
  1312. dev_err(&pdev->dev, "%s: swrm->reg_irq is NULL\n",
  1313. __func__);
  1314. ret = -EINVAL;
  1315. goto err_pdata_fail;
  1316. }
  1317. swrm->master.read = swrm_read;
  1318. swrm->master.write = swrm_write;
  1319. swrm->master.bulk_write = swrm_bulk_write;
  1320. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  1321. swrm->master.connect_port = swrm_connect_port;
  1322. swrm->master.disconnect_port = swrm_disconnect_port;
  1323. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  1324. swrm->master.remove_from_group = swrm_remove_from_group;
  1325. swrm->master.dev.parent = &pdev->dev;
  1326. swrm->master.dev.of_node = pdev->dev.of_node;
  1327. swrm->master.num_port = 0;
  1328. swrm->num_enum_slaves = 0;
  1329. swrm->rcmd_id = 0;
  1330. swrm->wcmd_id = 0;
  1331. swrm->slave_status = 0;
  1332. swrm->num_rx_chs = 0;
  1333. swrm->clk_ref_count = 0;
  1334. swrm->state = SWR_MSTR_RESUME;
  1335. init_completion(&swrm->reset);
  1336. init_completion(&swrm->broadcast);
  1337. mutex_init(&swrm->mlock);
  1338. INIT_LIST_HEAD(&swrm->mport_list);
  1339. mutex_init(&swrm->reslock);
  1340. mutex_init(&swrm->force_down_lock);
  1341. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  1342. &swrm->num_dev);
  1343. if (ret)
  1344. dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
  1345. __func__, "qcom,swr-num-dev");
  1346. else {
  1347. if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
  1348. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  1349. __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
  1350. ret = -EINVAL;
  1351. goto err_pdata_fail;
  1352. }
  1353. }
  1354. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  1355. SWR_IRQ_REGISTER);
  1356. if (ret) {
  1357. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  1358. __func__, ret);
  1359. goto err_irq_fail;
  1360. }
  1361. ret = swr_register_master(&swrm->master);
  1362. if (ret) {
  1363. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  1364. goto err_mstr_fail;
  1365. }
  1366. /* Add devices registered with board-info as the
  1367. * controller will be up now
  1368. */
  1369. swr_master_add_boarddevices(&swrm->master);
  1370. mutex_lock(&swrm->mlock);
  1371. swrm_clk_request(swrm, true);
  1372. ret = swrm_master_init(swrm);
  1373. if (ret < 0) {
  1374. dev_err(&pdev->dev,
  1375. "%s: Error in master Initializaiton, err %d\n",
  1376. __func__, ret);
  1377. mutex_unlock(&swrm->mlock);
  1378. goto err_mstr_fail;
  1379. }
  1380. swrm->version = swrm->read(swrm->handle, SWRM_COMP_HW_VERSION);
  1381. mutex_unlock(&swrm->mlock);
  1382. if (pdev->dev.of_node)
  1383. of_register_swr_devices(&swrm->master);
  1384. dbgswrm = swrm;
  1385. debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  1386. if (!IS_ERR(debugfs_swrm_dent)) {
  1387. debugfs_peek = debugfs_create_file("swrm_peek",
  1388. S_IFREG | 0444, debugfs_swrm_dent,
  1389. (void *) "swrm_peek", &swrm_debug_ops);
  1390. debugfs_poke = debugfs_create_file("swrm_poke",
  1391. S_IFREG | 0444, debugfs_swrm_dent,
  1392. (void *) "swrm_poke", &swrm_debug_ops);
  1393. debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  1394. S_IFREG | 0444, debugfs_swrm_dent,
  1395. (void *) "swrm_reg_dump",
  1396. &swrm_debug_ops);
  1397. }
  1398. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1399. pm_runtime_use_autosuspend(&pdev->dev);
  1400. pm_runtime_set_active(&pdev->dev);
  1401. pm_runtime_enable(&pdev->dev);
  1402. pm_runtime_mark_last_busy(&pdev->dev);
  1403. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  1404. swrm->event_notifier.notifier_call = swrm_event_notify;
  1405. msm_aud_evt_register_client(&swrm->event_notifier);
  1406. return 0;
  1407. err_mstr_fail:
  1408. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1409. swrm, SWR_IRQ_FREE);
  1410. err_irq_fail:
  1411. mutex_destroy(&swrm->mlock);
  1412. mutex_destroy(&swrm->reslock);
  1413. mutex_destroy(&swrm->force_down_lock);
  1414. err_pdata_fail:
  1415. kfree(swrm);
  1416. err_memory_fail:
  1417. return ret;
  1418. }
  1419. static int swrm_remove(struct platform_device *pdev)
  1420. {
  1421. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1422. if (swrm->reg_irq)
  1423. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1424. swrm, SWR_IRQ_FREE);
  1425. if (swrm->mstr_port) {
  1426. kfree(swrm->mstr_port->port);
  1427. swrm->mstr_port->port = NULL;
  1428. kfree(swrm->mstr_port);
  1429. swrm->mstr_port = NULL;
  1430. }
  1431. pm_runtime_disable(&pdev->dev);
  1432. pm_runtime_set_suspended(&pdev->dev);
  1433. swr_unregister_master(&swrm->master);
  1434. msm_aud_evt_unregister_client(&swrm->event_notifier);
  1435. mutex_destroy(&swrm->mlock);
  1436. mutex_destroy(&swrm->reslock);
  1437. mutex_destroy(&swrm->force_down_lock);
  1438. kfree(swrm);
  1439. return 0;
  1440. }
  1441. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  1442. {
  1443. u32 val;
  1444. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  1445. swrm->write(swrm->handle, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
  1446. val = swrm->read(swrm->handle, SWRM_MCP_CFG_ADDR);
  1447. val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
  1448. swrm->write(swrm->handle, SWRM_MCP_CFG_ADDR, val);
  1449. swrm->state = SWR_MSTR_PAUSE;
  1450. return 0;
  1451. }
  1452. #ifdef CONFIG_PM
  1453. static int swrm_runtime_resume(struct device *dev)
  1454. {
  1455. struct platform_device *pdev = to_platform_device(dev);
  1456. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1457. int ret = 0;
  1458. struct swr_master *mstr = &swrm->master;
  1459. struct swr_device *swr_dev;
  1460. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  1461. __func__, swrm->state);
  1462. mutex_lock(&swrm->reslock);
  1463. if ((swrm->state == SWR_MSTR_PAUSE) ||
  1464. (swrm->state == SWR_MSTR_DOWN)) {
  1465. if (swrm->state == SWR_MSTR_DOWN) {
  1466. if (swrm_clk_request(swrm, true))
  1467. goto exit;
  1468. }
  1469. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1470. ret = swr_device_up(swr_dev);
  1471. if (ret) {
  1472. dev_err(dev,
  1473. "%s: failed to wakeup swr dev %d\n",
  1474. __func__, swr_dev->dev_num);
  1475. swrm_clk_request(swrm, false);
  1476. goto exit;
  1477. }
  1478. }
  1479. swrm->write(swrm->handle, SWRM_COMP_SW_RESET, 0x01);
  1480. swrm->write(swrm->handle, SWRM_COMP_SW_RESET, 0x01);
  1481. swrm_master_init(swrm);
  1482. }
  1483. exit:
  1484. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1485. mutex_unlock(&swrm->reslock);
  1486. return ret;
  1487. }
  1488. static int swrm_runtime_suspend(struct device *dev)
  1489. {
  1490. struct platform_device *pdev = to_platform_device(dev);
  1491. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1492. int ret = 0;
  1493. struct swr_master *mstr = &swrm->master;
  1494. struct swr_device *swr_dev;
  1495. int current_state = 0;
  1496. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  1497. __func__, swrm->state);
  1498. mutex_lock(&swrm->reslock);
  1499. mutex_lock(&swrm->force_down_lock);
  1500. current_state = swrm->state;
  1501. mutex_unlock(&swrm->force_down_lock);
  1502. if ((current_state == SWR_MSTR_RESUME) ||
  1503. (current_state == SWR_MSTR_UP) ||
  1504. (current_state == SWR_MSTR_SSR)) {
  1505. if ((current_state != SWR_MSTR_SSR) &&
  1506. swrm_is_port_en(&swrm->master)) {
  1507. dev_dbg(dev, "%s ports are enabled\n", __func__);
  1508. ret = -EBUSY;
  1509. goto exit;
  1510. }
  1511. swrm_clk_pause(swrm);
  1512. swrm->write(swrm->handle, SWRM_COMP_CFG_ADDR, 0x00);
  1513. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1514. ret = swr_device_down(swr_dev);
  1515. if (ret) {
  1516. dev_err(dev,
  1517. "%s: failed to shutdown swr dev %d\n",
  1518. __func__, swr_dev->dev_num);
  1519. goto exit;
  1520. }
  1521. }
  1522. swrm_clk_request(swrm, false);
  1523. }
  1524. exit:
  1525. mutex_unlock(&swrm->reslock);
  1526. return ret;
  1527. }
  1528. #endif /* CONFIG_PM */
  1529. static int swrm_device_down(struct device *dev)
  1530. {
  1531. struct platform_device *pdev = to_platform_device(dev);
  1532. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1533. int ret = 0;
  1534. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  1535. mutex_lock(&swrm->force_down_lock);
  1536. swrm->state = SWR_MSTR_SSR;
  1537. mutex_unlock(&swrm->force_down_lock);
  1538. /* Use pm runtime function to tear down */
  1539. ret = pm_runtime_put_sync_suspend(dev);
  1540. pm_runtime_get_noresume(dev);
  1541. return ret;
  1542. }
  1543. /**
  1544. * swrm_wcd_notify - parent device can notify to soundwire master through
  1545. * this function
  1546. * @pdev: pointer to platform device structure
  1547. * @id: command id from parent to the soundwire master
  1548. * @data: data from parent device to soundwire master
  1549. */
  1550. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  1551. {
  1552. struct swr_mstr_ctrl *swrm;
  1553. int ret = 0;
  1554. struct swr_master *mstr;
  1555. struct swr_device *swr_dev;
  1556. if (!pdev) {
  1557. pr_err("%s: pdev is NULL\n", __func__);
  1558. return -EINVAL;
  1559. }
  1560. swrm = platform_get_drvdata(pdev);
  1561. if (!swrm) {
  1562. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  1563. return -EINVAL;
  1564. }
  1565. mstr = &swrm->master;
  1566. switch (id) {
  1567. case SWR_CH_MAP:
  1568. if (!data) {
  1569. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1570. ret = -EINVAL;
  1571. } else {
  1572. ret = swrm_set_ch_map(swrm, data);
  1573. }
  1574. break;
  1575. case SWR_DEVICE_DOWN:
  1576. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  1577. mutex_lock(&swrm->mlock);
  1578. if ((swrm->state == SWR_MSTR_PAUSE) ||
  1579. (swrm->state == SWR_MSTR_DOWN))
  1580. dev_dbg(swrm->dev, "%s: SWR master is already Down: %d\n",
  1581. __func__, swrm->state);
  1582. else
  1583. swrm_device_down(&pdev->dev);
  1584. mutex_unlock(&swrm->mlock);
  1585. break;
  1586. case SWR_DEVICE_UP:
  1587. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  1588. mutex_lock(&swrm->mlock);
  1589. mutex_lock(&swrm->reslock);
  1590. if ((swrm->state == SWR_MSTR_RESUME) ||
  1591. (swrm->state == SWR_MSTR_UP)) {
  1592. dev_dbg(swrm->dev, "%s: SWR master is already UP: %d\n",
  1593. __func__, swrm->state);
  1594. list_for_each_entry(swr_dev, &mstr->devices, dev_list)
  1595. swr_reset_device(swr_dev);
  1596. } else {
  1597. pm_runtime_mark_last_busy(&pdev->dev);
  1598. mutex_unlock(&swrm->reslock);
  1599. pm_runtime_get_sync(&pdev->dev);
  1600. mutex_lock(&swrm->reslock);
  1601. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1602. ret = swr_reset_device(swr_dev);
  1603. if (ret) {
  1604. dev_err(swrm->dev,
  1605. "%s: failed to reset swr device %d\n",
  1606. __func__, swr_dev->dev_num);
  1607. swrm_clk_request(swrm, false);
  1608. }
  1609. }
  1610. pm_runtime_mark_last_busy(&pdev->dev);
  1611. pm_runtime_put_autosuspend(&pdev->dev);
  1612. }
  1613. mutex_unlock(&swrm->reslock);
  1614. mutex_unlock(&swrm->mlock);
  1615. break;
  1616. case SWR_SET_NUM_RX_CH:
  1617. if (!data) {
  1618. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1619. ret = -EINVAL;
  1620. } else {
  1621. mutex_lock(&swrm->mlock);
  1622. swrm->num_rx_chs = *(int *)data;
  1623. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  1624. list_for_each_entry(swr_dev, &mstr->devices,
  1625. dev_list) {
  1626. ret = swr_set_device_group(swr_dev,
  1627. SWR_BROADCAST);
  1628. if (ret)
  1629. dev_err(swrm->dev,
  1630. "%s: set num ch failed\n",
  1631. __func__);
  1632. }
  1633. } else {
  1634. list_for_each_entry(swr_dev, &mstr->devices,
  1635. dev_list) {
  1636. ret = swr_set_device_group(swr_dev,
  1637. SWR_GROUP_NONE);
  1638. if (ret)
  1639. dev_err(swrm->dev,
  1640. "%s: set num ch failed\n",
  1641. __func__);
  1642. }
  1643. }
  1644. mutex_unlock(&swrm->mlock);
  1645. }
  1646. break;
  1647. default:
  1648. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  1649. __func__, id);
  1650. break;
  1651. }
  1652. return ret;
  1653. }
  1654. EXPORT_SYMBOL(swrm_wcd_notify);
  1655. #ifdef CONFIG_PM_SLEEP
  1656. static int swrm_suspend(struct device *dev)
  1657. {
  1658. int ret = -EBUSY;
  1659. struct platform_device *pdev = to_platform_device(dev);
  1660. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1661. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  1662. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  1663. ret = swrm_runtime_suspend(dev);
  1664. if (!ret) {
  1665. /*
  1666. * Synchronize runtime-pm and system-pm states:
  1667. * At this point, we are already suspended. If
  1668. * runtime-pm still thinks its active, then
  1669. * make sure its status is in sync with HW
  1670. * status. The three below calls let the
  1671. * runtime-pm know that we are suspended
  1672. * already without re-invoking the suspend
  1673. * callback
  1674. */
  1675. pm_runtime_disable(dev);
  1676. pm_runtime_set_suspended(dev);
  1677. pm_runtime_enable(dev);
  1678. }
  1679. }
  1680. if (ret == -EBUSY) {
  1681. /*
  1682. * There is a possibility that some audio stream is active
  1683. * during suspend. We dont want to return suspend failure in
  1684. * that case so that display and relevant components can still
  1685. * go to suspend.
  1686. * If there is some other error, then it should be passed-on
  1687. * to system level suspend
  1688. */
  1689. ret = 0;
  1690. }
  1691. return ret;
  1692. }
  1693. static int swrm_resume(struct device *dev)
  1694. {
  1695. int ret = 0;
  1696. struct platform_device *pdev = to_platform_device(dev);
  1697. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1698. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  1699. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  1700. ret = swrm_runtime_resume(dev);
  1701. if (!ret) {
  1702. pm_runtime_mark_last_busy(dev);
  1703. pm_request_autosuspend(dev);
  1704. }
  1705. }
  1706. return ret;
  1707. }
  1708. #endif /* CONFIG_PM_SLEEP */
  1709. static const struct dev_pm_ops swrm_dev_pm_ops = {
  1710. SET_SYSTEM_SLEEP_PM_OPS(
  1711. swrm_suspend,
  1712. swrm_resume
  1713. )
  1714. SET_RUNTIME_PM_OPS(
  1715. swrm_runtime_suspend,
  1716. swrm_runtime_resume,
  1717. NULL
  1718. )
  1719. };
  1720. static const struct of_device_id swrm_dt_match[] = {
  1721. {
  1722. .compatible = "qcom,swr-wcd",
  1723. },
  1724. {}
  1725. };
  1726. static struct platform_driver swr_mstr_driver = {
  1727. .probe = swrm_probe,
  1728. .remove = swrm_remove,
  1729. .driver = {
  1730. .name = SWR_WCD_NAME,
  1731. .owner = THIS_MODULE,
  1732. .pm = &swrm_dev_pm_ops,
  1733. .of_match_table = swrm_dt_match,
  1734. },
  1735. };
  1736. static int __init swrm_init(void)
  1737. {
  1738. return platform_driver_register(&swr_mstr_driver);
  1739. }
  1740. module_init(swrm_init);
  1741. static void __exit swrm_exit(void)
  1742. {
  1743. platform_driver_unregister(&swr_mstr_driver);
  1744. }
  1745. module_exit(swrm_exit);
  1746. MODULE_LICENSE("GPL v2");
  1747. MODULE_DESCRIPTION("WCD SoundWire Controller");
  1748. MODULE_ALIAS("platform:swr-wcd");