swr-mstr-ctrl.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/irq.h>
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/delay.h>
  13. #include <linux/kthread.h>
  14. #include <linux/bitops.h>
  15. #include <linux/clk.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/of.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/uaccess.h>
  20. #include <soc/soundwire.h>
  21. #include <soc/swr-wcd.h>
  22. #include <linux/regmap.h>
  23. #include <dsp/msm-audio-event-notify.h>
  24. #include "swrm_registers.h"
  25. #include "swr-mstr-ctrl.h"
  26. #include "swrm_port_config.h"
  27. #define SWR_BROADCAST_CMD_ID 0x0F
  28. #define SWR_AUTO_SUSPEND_DELAY 3 /* delay in sec */
  29. #define SWR_DEV_ID_MASK 0xFFFFFFFF
  30. #define SWR_REG_VAL_PACK(data, dev, id, reg) \
  31. ((reg) | ((id) << 16) | ((dev) << 20) | ((data) << 24))
  32. #define SWR_INVALID_PARAM 0xFF
  33. #define SWR_HSTOP_MAX_VAL 0xF
  34. #define SWR_HSTART_MIN_VAL 0x0
  35. #define SWRM_INTERRUPT_STATUS_MASK 0x1FDFD
  36. /* pm runtime auto suspend timer in msecs */
  37. static int auto_suspend_timer = SWR_AUTO_SUSPEND_DELAY * 1000;
  38. module_param(auto_suspend_timer, int, 0664);
  39. MODULE_PARM_DESC(auto_suspend_timer, "timer for auto suspend");
  40. enum {
  41. SWR_NOT_PRESENT, /* Device is detached/not present on the bus */
  42. SWR_ATTACHED_OK, /* Device is attached */
  43. SWR_ALERT, /* Device alters master for any interrupts */
  44. SWR_RESERVED, /* Reserved */
  45. };
  46. enum {
  47. MASTER_ID_WSA = 1,
  48. MASTER_ID_RX,
  49. MASTER_ID_TX
  50. };
  51. enum {
  52. ENABLE_PENDING,
  53. DISABLE_PENDING
  54. };
  55. #define TRUE 1
  56. #define FALSE 0
  57. #define SWRM_MAX_PORT_REG 120
  58. #define SWRM_MAX_INIT_REG 11
  59. #define SWR_MSTR_MAX_REG_ADDR 0x1740
  60. #define SWR_MSTR_START_REG_ADDR 0x00
  61. #define SWR_MSTR_MAX_BUF_LEN 32
  62. #define BYTES_PER_LINE 12
  63. #define SWR_MSTR_RD_BUF_LEN 8
  64. #define SWR_MSTR_WR_BUF_LEN 32
  65. #define MAX_FIFO_RD_FAIL_RETRY 3
  66. static struct swr_mstr_ctrl *dbgswrm;
  67. static struct dentry *debugfs_swrm_dent;
  68. static struct dentry *debugfs_peek;
  69. static struct dentry *debugfs_poke;
  70. static struct dentry *debugfs_reg_dump;
  71. static unsigned int read_data;
  72. static bool swrm_is_msm_variant(int val)
  73. {
  74. return (val == SWRM_VERSION_1_3);
  75. }
  76. static int swrm_debug_open(struct inode *inode, struct file *file)
  77. {
  78. file->private_data = inode->i_private;
  79. return 0;
  80. }
  81. static int get_parameters(char *buf, u32 *param1, int num_of_par)
  82. {
  83. char *token;
  84. int base, cnt;
  85. token = strsep(&buf, " ");
  86. for (cnt = 0; cnt < num_of_par; cnt++) {
  87. if (token) {
  88. if ((token[1] == 'x') || (token[1] == 'X'))
  89. base = 16;
  90. else
  91. base = 10;
  92. if (kstrtou32(token, base, &param1[cnt]) != 0)
  93. return -EINVAL;
  94. token = strsep(&buf, " ");
  95. } else
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. static ssize_t swrm_reg_show(char __user *ubuf, size_t count,
  101. loff_t *ppos)
  102. {
  103. int i, reg_val, len;
  104. ssize_t total = 0;
  105. char tmp_buf[SWR_MSTR_MAX_BUF_LEN];
  106. if (!ubuf || !ppos)
  107. return 0;
  108. for (i = (((int) *ppos / BYTES_PER_LINE) + SWR_MSTR_START_REG_ADDR);
  109. i <= SWR_MSTR_MAX_REG_ADDR; i += 4) {
  110. reg_val = dbgswrm->read(dbgswrm->handle, i);
  111. len = snprintf(tmp_buf, 25, "0x%.3x: 0x%.2x\n", i, reg_val);
  112. if ((total + len) >= count - 1)
  113. break;
  114. if (copy_to_user((ubuf + total), tmp_buf, len)) {
  115. pr_err("%s: fail to copy reg dump\n", __func__);
  116. total = -EFAULT;
  117. goto copy_err;
  118. }
  119. *ppos += len;
  120. total += len;
  121. }
  122. copy_err:
  123. return total;
  124. }
  125. static ssize_t swrm_debug_read(struct file *file, char __user *ubuf,
  126. size_t count, loff_t *ppos)
  127. {
  128. char lbuf[SWR_MSTR_RD_BUF_LEN];
  129. char *access_str;
  130. ssize_t ret_cnt;
  131. if (!count || !file || !ppos || !ubuf)
  132. return -EINVAL;
  133. access_str = file->private_data;
  134. if (*ppos < 0)
  135. return -EINVAL;
  136. if (!strcmp(access_str, "swrm_peek")) {
  137. snprintf(lbuf, sizeof(lbuf), "0x%x\n", read_data);
  138. ret_cnt = simple_read_from_buffer(ubuf, count, ppos, lbuf,
  139. strnlen(lbuf, 7));
  140. } else if (!strcmp(access_str, "swrm_reg_dump")) {
  141. ret_cnt = swrm_reg_show(ubuf, count, ppos);
  142. } else {
  143. pr_err("%s: %s not permitted to read\n", __func__, access_str);
  144. ret_cnt = -EPERM;
  145. }
  146. return ret_cnt;
  147. }
  148. static ssize_t swrm_debug_write(struct file *filp,
  149. const char __user *ubuf, size_t cnt, loff_t *ppos)
  150. {
  151. char lbuf[SWR_MSTR_WR_BUF_LEN];
  152. int rc;
  153. u32 param[5];
  154. char *access_str;
  155. if (!filp || !ppos || !ubuf)
  156. return -EINVAL;
  157. access_str = filp->private_data;
  158. if (cnt > sizeof(lbuf) - 1)
  159. return -EINVAL;
  160. rc = copy_from_user(lbuf, ubuf, cnt);
  161. if (rc)
  162. return -EFAULT;
  163. lbuf[cnt] = '\0';
  164. if (!strcmp(access_str, "swrm_poke")) {
  165. /* write */
  166. rc = get_parameters(lbuf, param, 2);
  167. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) &&
  168. (param[1] <= 0xFFFFFFFF) &&
  169. (rc == 0))
  170. rc = dbgswrm->write(dbgswrm->handle, param[0],
  171. param[1]);
  172. else
  173. rc = -EINVAL;
  174. } else if (!strcmp(access_str, "swrm_peek")) {
  175. /* read */
  176. rc = get_parameters(lbuf, param, 1);
  177. if ((param[0] <= SWR_MSTR_MAX_REG_ADDR) && (rc == 0))
  178. read_data = dbgswrm->read(dbgswrm->handle, param[0]);
  179. else
  180. rc = -EINVAL;
  181. }
  182. if (rc == 0)
  183. rc = cnt;
  184. else
  185. pr_err("%s: rc = %d\n", __func__, rc);
  186. return rc;
  187. }
  188. static const struct file_operations swrm_debug_ops = {
  189. .open = swrm_debug_open,
  190. .write = swrm_debug_write,
  191. .read = swrm_debug_read,
  192. };
  193. static int swrm_clk_request(struct swr_mstr_ctrl *swrm, bool enable)
  194. {
  195. int ret = 0;
  196. if (!swrm->clk || !swrm->handle)
  197. return -EINVAL;
  198. mutex_lock(&swrm->clklock);
  199. if (enable) {
  200. if (!swrm->dev_up)
  201. goto exit;
  202. swrm->clk_ref_count++;
  203. if (swrm->clk_ref_count == 1) {
  204. ret = swrm->clk(swrm->handle, true);
  205. if (ret) {
  206. dev_err(swrm->dev,
  207. "%s: clock enable req failed",
  208. __func__);
  209. --swrm->clk_ref_count;
  210. }
  211. }
  212. } else if (--swrm->clk_ref_count == 0) {
  213. swrm->clk(swrm->handle, false);
  214. complete(&swrm->clk_off_complete);
  215. }
  216. if (swrm->clk_ref_count < 0) {
  217. pr_err("%s: swrm clk count mismatch\n", __func__);
  218. swrm->clk_ref_count = 0;
  219. }
  220. exit:
  221. mutex_unlock(&swrm->clklock);
  222. return ret;
  223. }
  224. static int swrm_ahb_write(struct swr_mstr_ctrl *swrm,
  225. u16 reg, u32 *value)
  226. {
  227. u32 temp = (u32)(*value);
  228. int ret = 0;
  229. mutex_lock(&swrm->devlock);
  230. if (!swrm->dev_up)
  231. goto err;
  232. ret = swrm_clk_request(swrm, TRUE);
  233. if (ret) {
  234. dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
  235. __func__);
  236. goto err;
  237. }
  238. iowrite32(temp, swrm->swrm_dig_base + reg);
  239. swrm_clk_request(swrm, FALSE);
  240. err:
  241. mutex_unlock(&swrm->devlock);
  242. return ret;
  243. }
  244. static int swrm_ahb_read(struct swr_mstr_ctrl *swrm,
  245. u16 reg, u32 *value)
  246. {
  247. u32 temp = 0;
  248. int ret = 0;
  249. mutex_lock(&swrm->devlock);
  250. if (!swrm->dev_up)
  251. goto err;
  252. ret = swrm_clk_request(swrm, TRUE);
  253. if (ret) {
  254. dev_err_ratelimited(swrm->dev, "%s: clock request failed\n",
  255. __func__);
  256. goto err;
  257. }
  258. temp = ioread32(swrm->swrm_dig_base + reg);
  259. *value = temp;
  260. swrm_clk_request(swrm, FALSE);
  261. err:
  262. mutex_unlock(&swrm->devlock);
  263. return ret;
  264. }
  265. static u32 swr_master_read(struct swr_mstr_ctrl *swrm, unsigned int reg_addr)
  266. {
  267. u32 val = 0;
  268. if (swrm->read)
  269. val = swrm->read(swrm->handle, reg_addr);
  270. else
  271. swrm_ahb_read(swrm, reg_addr, &val);
  272. return val;
  273. }
  274. static void swr_master_write(struct swr_mstr_ctrl *swrm, u16 reg_addr, u32 val)
  275. {
  276. if (swrm->write)
  277. swrm->write(swrm->handle, reg_addr, val);
  278. else
  279. swrm_ahb_write(swrm, reg_addr, &val);
  280. }
  281. static int swr_master_bulk_write(struct swr_mstr_ctrl *swrm, u32 *reg_addr,
  282. u32 *val, unsigned int length)
  283. {
  284. int i = 0;
  285. if (swrm->bulk_write)
  286. swrm->bulk_write(swrm->handle, reg_addr, val, length);
  287. else {
  288. mutex_lock(&swrm->iolock);
  289. for (i = 0; i < length; i++) {
  290. /* wait for FIFO WR command to complete to avoid overflow */
  291. usleep_range(100, 105);
  292. swr_master_write(swrm, reg_addr[i], val[i]);
  293. }
  294. mutex_unlock(&swrm->iolock);
  295. }
  296. return 0;
  297. }
  298. static bool swrm_is_port_en(struct swr_master *mstr)
  299. {
  300. return !!(mstr->num_port);
  301. }
  302. static void copy_port_tables(struct swr_mstr_ctrl *swrm,
  303. struct port_params *params)
  304. {
  305. u8 i;
  306. struct port_params *config = params;
  307. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  308. /* wsa uses single frame structure for all configurations */
  309. if (!swrm->mport_cfg[i].port_en)
  310. continue;
  311. swrm->mport_cfg[i].sinterval = config[i].si;
  312. swrm->mport_cfg[i].offset1 = config[i].off1;
  313. swrm->mport_cfg[i].offset2 = config[i].off2;
  314. swrm->mport_cfg[i].hstart = config[i].hstart;
  315. swrm->mport_cfg[i].hstop = config[i].hstop;
  316. swrm->mport_cfg[i].blk_pack_mode = config[i].bp_mode;
  317. swrm->mport_cfg[i].blk_grp_count = config[i].bgp_ctrl;
  318. swrm->mport_cfg[i].word_length = config[i].wd_len;
  319. swrm->mport_cfg[i].lane_ctrl = config[i].lane_ctrl;
  320. }
  321. }
  322. static int swrm_get_port_config(struct swr_mstr_ctrl *swrm)
  323. {
  324. struct port_params *params;
  325. switch (swrm->master_id) {
  326. case MASTER_ID_WSA:
  327. params = wsa_frame_superset;
  328. break;
  329. case MASTER_ID_RX:
  330. /* Two RX tables for dsd and without dsd enabled */
  331. if (swrm->mport_cfg[4].port_en)
  332. params = rx_frame_params_dsd;
  333. else
  334. params = rx_frame_params;
  335. break;
  336. case MASTER_ID_TX:
  337. params = tx_frame_params_superset;
  338. break;
  339. default: /* MASTER_GENERIC*/
  340. /* computer generic frame parameters */
  341. return -EINVAL;
  342. }
  343. copy_port_tables(swrm, params);
  344. return 0;
  345. }
  346. static int swrm_get_master_port(struct swr_mstr_ctrl *swrm, u8 *mstr_port_id,
  347. u8 *mstr_ch_mask, u8 mstr_prt_type,
  348. u8 slv_port_id)
  349. {
  350. int i, j;
  351. *mstr_port_id = 0;
  352. for (i = 1; i <= swrm->num_ports; i++) {
  353. for (j = 0; j < SWR_MAX_CH_PER_PORT; j++) {
  354. if (swrm->port_mapping[i][j].port_type == mstr_prt_type)
  355. goto found;
  356. }
  357. }
  358. found:
  359. if (i > swrm->num_ports || j == SWR_MAX_CH_PER_PORT) {
  360. dev_err(swrm->dev, "%s: port type not supported by master\n",
  361. __func__);
  362. return -EINVAL;
  363. }
  364. /* id 0 corresponds to master port 1 */
  365. *mstr_port_id = i - 1;
  366. *mstr_ch_mask = swrm->port_mapping[i][j].ch_mask;
  367. return 0;
  368. }
  369. static u32 swrm_get_packed_reg_val(u8 *cmd_id, u8 cmd_data,
  370. u8 dev_addr, u16 reg_addr)
  371. {
  372. u32 val;
  373. u8 id = *cmd_id;
  374. if (id != SWR_BROADCAST_CMD_ID) {
  375. if (id < 14)
  376. id += 1;
  377. else
  378. id = 0;
  379. *cmd_id = id;
  380. }
  381. val = SWR_REG_VAL_PACK(cmd_data, dev_addr, id, reg_addr);
  382. return val;
  383. }
  384. static int swrm_cmd_fifo_rd_cmd(struct swr_mstr_ctrl *swrm, int *cmd_data,
  385. u8 dev_addr, u8 cmd_id, u16 reg_addr,
  386. u32 len)
  387. {
  388. u32 val;
  389. u32 retry_attempt = 0;
  390. mutex_lock(&swrm->iolock);
  391. val = swrm_get_packed_reg_val(&swrm->rcmd_id, len, dev_addr, reg_addr);
  392. /* wait for FIFO RD to complete to avoid overflow */
  393. usleep_range(100, 105);
  394. swr_master_write(swrm, SWRM_CMD_FIFO_RD_CMD, val);
  395. /* wait for FIFO RD CMD complete to avoid overflow */
  396. usleep_range(250, 255);
  397. retry_read:
  398. *cmd_data = swr_master_read(swrm, SWRM_CMD_FIFO_RD_FIFO_ADDR);
  399. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, rcmd_id: 0x%x, \
  400. dev_num: 0x%x, cmd_data: 0x%x\n", __func__, reg_addr,
  401. cmd_id, swrm->rcmd_id, dev_addr, *cmd_data);
  402. if ((((*cmd_data) & 0xF00) >> 8) != swrm->rcmd_id) {
  403. if (retry_attempt < MAX_FIFO_RD_FAIL_RETRY) {
  404. /* wait 500 us before retry on fifo read failure */
  405. usleep_range(500, 505);
  406. retry_attempt++;
  407. goto retry_read;
  408. } else {
  409. dev_err_ratelimited(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x, \
  410. rcmd_id: 0x%x, dev_num: 0x%x, cmd_data: 0x%x\n",
  411. __func__, reg_addr, cmd_id, swrm->rcmd_id,
  412. dev_addr, *cmd_data);
  413. dev_err_ratelimited(swrm->dev,
  414. "%s: failed to read fifo\n", __func__);
  415. }
  416. }
  417. mutex_unlock(&swrm->iolock);
  418. return 0;
  419. }
  420. static int swrm_cmd_fifo_wr_cmd(struct swr_mstr_ctrl *swrm, u8 cmd_data,
  421. u8 dev_addr, u8 cmd_id, u16 reg_addr)
  422. {
  423. u32 val;
  424. int ret = 0;
  425. mutex_lock(&swrm->iolock);
  426. if (!cmd_id)
  427. val = swrm_get_packed_reg_val(&swrm->wcmd_id, cmd_data,
  428. dev_addr, reg_addr);
  429. else
  430. val = swrm_get_packed_reg_val(&cmd_id, cmd_data,
  431. dev_addr, reg_addr);
  432. dev_dbg(swrm->dev, "%s: reg: 0x%x, cmd_id: 0x%x,wcmd_id: 0x%x, \
  433. dev_num: 0x%x, cmd_data: 0x%x\n", __func__,
  434. reg_addr, cmd_id, swrm->wcmd_id,dev_addr, cmd_data);
  435. /* wait for FIFO WR command to complete to avoid overflow */
  436. usleep_range(250, 255);
  437. swr_master_write(swrm, SWRM_CMD_FIFO_WR_CMD, val);
  438. if (cmd_id == 0xF) {
  439. /*
  440. * sleep for 10ms for MSM soundwire variant to allow broadcast
  441. * command to complete.
  442. */
  443. if (swrm_is_msm_variant(swrm->version))
  444. usleep_range(10000, 10100);
  445. else
  446. wait_for_completion_timeout(&swrm->broadcast,
  447. (2 * HZ/10));
  448. }
  449. mutex_unlock(&swrm->iolock);
  450. return ret;
  451. }
  452. static int swrm_read(struct swr_master *master, u8 dev_num, u16 reg_addr,
  453. void *buf, u32 len)
  454. {
  455. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  456. int ret = 0;
  457. int val;
  458. u8 *reg_val = (u8 *)buf;
  459. if (!swrm) {
  460. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  461. return -EINVAL;
  462. }
  463. if (!dev_num) {
  464. dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
  465. return -EINVAL;
  466. }
  467. mutex_lock(&swrm->devlock);
  468. if (!swrm->dev_up) {
  469. mutex_unlock(&swrm->devlock);
  470. return 0;
  471. }
  472. mutex_unlock(&swrm->devlock);
  473. pm_runtime_get_sync(swrm->dev);
  474. ret = swrm_cmd_fifo_rd_cmd(swrm, &val, dev_num, 0, reg_addr, len);
  475. if (!ret)
  476. *reg_val = (u8)val;
  477. pm_runtime_put_autosuspend(swrm->dev);
  478. pm_runtime_mark_last_busy(swrm->dev);
  479. return ret;
  480. }
  481. static int swrm_write(struct swr_master *master, u8 dev_num, u16 reg_addr,
  482. const void *buf)
  483. {
  484. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  485. int ret = 0;
  486. u8 reg_val = *(u8 *)buf;
  487. if (!swrm) {
  488. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  489. return -EINVAL;
  490. }
  491. if (!dev_num) {
  492. dev_err(&master->dev, "%s: invalid slave dev num\n", __func__);
  493. return -EINVAL;
  494. }
  495. mutex_lock(&swrm->devlock);
  496. if (!swrm->dev_up) {
  497. mutex_unlock(&swrm->devlock);
  498. return 0;
  499. }
  500. mutex_unlock(&swrm->devlock);
  501. pm_runtime_get_sync(swrm->dev);
  502. ret = swrm_cmd_fifo_wr_cmd(swrm, reg_val, dev_num, 0, reg_addr);
  503. pm_runtime_put_autosuspend(swrm->dev);
  504. pm_runtime_mark_last_busy(swrm->dev);
  505. return ret;
  506. }
  507. static int swrm_bulk_write(struct swr_master *master, u8 dev_num, void *reg,
  508. const void *buf, size_t len)
  509. {
  510. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  511. int ret = 0;
  512. int i;
  513. u32 *val;
  514. u32 *swr_fifo_reg;
  515. if (!swrm || !swrm->handle) {
  516. dev_err(&master->dev, "%s: swrm is NULL\n", __func__);
  517. return -EINVAL;
  518. }
  519. if (len <= 0)
  520. return -EINVAL;
  521. mutex_lock(&swrm->devlock);
  522. if (!swrm->dev_up) {
  523. mutex_unlock(&swrm->devlock);
  524. return 0;
  525. }
  526. mutex_unlock(&swrm->devlock);
  527. pm_runtime_get_sync(swrm->dev);
  528. if (dev_num) {
  529. swr_fifo_reg = kcalloc(len, sizeof(u32), GFP_KERNEL);
  530. if (!swr_fifo_reg) {
  531. ret = -ENOMEM;
  532. goto err;
  533. }
  534. val = kcalloc(len, sizeof(u32), GFP_KERNEL);
  535. if (!val) {
  536. ret = -ENOMEM;
  537. goto mem_fail;
  538. }
  539. for (i = 0; i < len; i++) {
  540. val[i] = swrm_get_packed_reg_val(&swrm->wcmd_id,
  541. ((u8 *)buf)[i],
  542. dev_num,
  543. ((u16 *)reg)[i]);
  544. swr_fifo_reg[i] = SWRM_CMD_FIFO_WR_CMD;
  545. }
  546. ret = swr_master_bulk_write(swrm, swr_fifo_reg, val, len);
  547. if (ret) {
  548. dev_err(&master->dev, "%s: bulk write failed\n",
  549. __func__);
  550. ret = -EINVAL;
  551. }
  552. } else {
  553. dev_err(&master->dev,
  554. "%s: No support of Bulk write for master regs\n",
  555. __func__);
  556. ret = -EINVAL;
  557. goto err;
  558. }
  559. kfree(val);
  560. mem_fail:
  561. kfree(swr_fifo_reg);
  562. err:
  563. pm_runtime_put_autosuspend(swrm->dev);
  564. pm_runtime_mark_last_busy(swrm->dev);
  565. return ret;
  566. }
  567. static u8 get_inactive_bank_num(struct swr_mstr_ctrl *swrm)
  568. {
  569. return (swr_master_read(swrm, SWRM_MCP_STATUS) &
  570. SWRM_MCP_STATUS_BANK_NUM_MASK) ? 0 : 1;
  571. }
  572. static void enable_bank_switch(struct swr_mstr_ctrl *swrm, u8 bank,
  573. u8 row, u8 col)
  574. {
  575. swrm_cmd_fifo_wr_cmd(swrm, ((row << 3) | col), 0xF, 0xF,
  576. SWRS_SCP_FRAME_CTRL_BANK(bank));
  577. }
  578. static struct swr_port_info *swrm_get_port_req(struct swrm_mports *mport,
  579. u8 slv_port, u8 dev_num)
  580. {
  581. struct swr_port_info *port_req = NULL;
  582. list_for_each_entry(port_req, &mport->port_req_list, list) {
  583. /* Store dev_id instead of dev_num if enumeration is changed run_time */
  584. if ((port_req->slave_port_id == slv_port)
  585. && (port_req->dev_num == dev_num))
  586. return port_req;
  587. }
  588. return NULL;
  589. }
  590. static bool swrm_remove_from_group(struct swr_master *master)
  591. {
  592. struct swr_device *swr_dev;
  593. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  594. bool is_removed = false;
  595. if (!swrm)
  596. goto end;
  597. mutex_lock(&swrm->mlock);
  598. if ((swrm->num_rx_chs > 1) &&
  599. (swrm->num_rx_chs == swrm->num_cfg_devs)) {
  600. list_for_each_entry(swr_dev, &master->devices,
  601. dev_list) {
  602. swr_dev->group_id = SWR_GROUP_NONE;
  603. master->gr_sid = 0;
  604. }
  605. is_removed = true;
  606. }
  607. mutex_unlock(&swrm->mlock);
  608. end:
  609. return is_removed;
  610. }
  611. static void swrm_disable_ports(struct swr_master *master,
  612. u8 bank)
  613. {
  614. u32 value;
  615. struct swr_port_info *port_req;
  616. int i;
  617. struct swrm_mports *mport;
  618. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  619. if (!swrm) {
  620. pr_err("%s: swrm is null\n", __func__);
  621. return;
  622. }
  623. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  624. master->num_port);
  625. for (i = 0; i < SWR_MSTR_PORT_LEN ; i++) {
  626. mport = &(swrm->mport_cfg[i]);
  627. if (!mport->port_en)
  628. continue;
  629. list_for_each_entry(port_req, &mport->port_req_list, list) {
  630. /* skip ports with no change req's*/
  631. if (port_req->req_ch == port_req->ch_en)
  632. continue;
  633. swrm_cmd_fifo_wr_cmd(swrm, port_req->req_ch,
  634. port_req->dev_num, 0x00,
  635. SWRS_DP_CHANNEL_ENABLE_BANK(port_req->slave_port_id,
  636. bank));
  637. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x\n",
  638. __func__, i,
  639. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)));
  640. }
  641. value = ((mport->req_ch)
  642. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  643. value |= ((mport->offset2)
  644. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  645. value |= ((mport->offset1)
  646. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  647. value |= mport->sinterval;
  648. swr_master_write(swrm,
  649. SWRM_DP_PORT_CTRL_BANK(i+1, bank),
  650. value);
  651. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  652. __func__, i,
  653. (SWRM_DP_PORT_CTRL_BANK(i+1, bank)), value);
  654. }
  655. }
  656. static void swrm_cleanup_disabled_port_reqs(struct swr_master *master)
  657. {
  658. struct swr_port_info *port_req, *next;
  659. int i;
  660. struct swrm_mports *mport;
  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. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  669. mport = &(swrm->mport_cfg[i]);
  670. list_for_each_entry_safe(port_req, next,
  671. &mport->port_req_list, list) {
  672. /* skip ports without new ch req */
  673. if (port_req->ch_en == port_req->req_ch)
  674. continue;
  675. /* remove new ch req's*/
  676. port_req->ch_en = port_req->req_ch;
  677. /* If no streams enabled on port, remove the port req */
  678. if (port_req->ch_en == 0) {
  679. list_del(&port_req->list);
  680. kfree(port_req);
  681. }
  682. }
  683. /* remove new ch req's on mport*/
  684. mport->ch_en = mport->req_ch;
  685. if (!(mport->ch_en)) {
  686. mport->port_en = false;
  687. master->port_en_mask &= ~i;
  688. }
  689. }
  690. }
  691. static void swrm_copy_data_port_config(struct swr_master *master, u8 bank)
  692. {
  693. u32 value, slv_id;
  694. struct swr_port_info *port_req;
  695. int i;
  696. struct swrm_mports *mport;
  697. u32 reg[SWRM_MAX_PORT_REG];
  698. u32 val[SWRM_MAX_PORT_REG];
  699. int len = 0;
  700. u8 hparams;
  701. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  702. if (!swrm) {
  703. pr_err("%s: swrm is null\n", __func__);
  704. return;
  705. }
  706. dev_dbg(swrm->dev, "%s: master num_port: %d\n", __func__,
  707. master->num_port);
  708. for (i = 0; i < SWR_MSTR_PORT_LEN; i++) {
  709. mport = &(swrm->mport_cfg[i]);
  710. if (!mport->port_en)
  711. continue;
  712. list_for_each_entry(port_req, &mport->port_req_list, list) {
  713. slv_id = port_req->slave_port_id;
  714. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  715. val[len++] = SWR_REG_VAL_PACK(port_req->req_ch,
  716. port_req->dev_num, 0x00,
  717. SWRS_DP_CHANNEL_ENABLE_BANK(slv_id,
  718. bank));
  719. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  720. val[len++] = SWR_REG_VAL_PACK(mport->sinterval,
  721. port_req->dev_num, 0x00,
  722. SWRS_DP_SAMPLE_CONTROL_1_BANK(slv_id,
  723. bank));
  724. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  725. val[len++] = SWR_REG_VAL_PACK(mport->offset1,
  726. port_req->dev_num, 0x00,
  727. SWRS_DP_OFFSET_CONTROL_1_BANK(slv_id,
  728. bank));
  729. if (mport->offset2 != SWR_INVALID_PARAM) {
  730. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  731. val[len++] = SWR_REG_VAL_PACK(mport->offset2,
  732. port_req->dev_num, 0x00,
  733. SWRS_DP_OFFSET_CONTROL_2_BANK(
  734. slv_id, bank));
  735. }
  736. if (mport->hstart != SWR_INVALID_PARAM
  737. && mport->hstop != SWR_INVALID_PARAM) {
  738. hparams = (mport->hstart << 4) | mport->hstop;
  739. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  740. val[len++] = SWR_REG_VAL_PACK(hparams,
  741. port_req->dev_num, 0x00,
  742. SWRS_DP_HCONTROL_BANK(slv_id,
  743. bank));
  744. }
  745. if (mport->word_length != SWR_INVALID_PARAM) {
  746. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  747. val[len++] =
  748. SWR_REG_VAL_PACK(mport->word_length,
  749. port_req->dev_num, 0x00,
  750. SWRS_DP_BLOCK_CONTROL_1(slv_id));
  751. }
  752. if (mport->blk_pack_mode != SWR_INVALID_PARAM
  753. && swrm->master_id != MASTER_ID_WSA) {
  754. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  755. val[len++] =
  756. SWR_REG_VAL_PACK(mport->blk_pack_mode,
  757. port_req->dev_num, 0x00,
  758. SWRS_DP_BLOCK_CONTROL_3_BANK(slv_id,
  759. bank));
  760. }
  761. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  762. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  763. val[len++] =
  764. SWR_REG_VAL_PACK(mport->blk_grp_count,
  765. port_req->dev_num, 0x00,
  766. SWRS_DP_BLOCK_CONTROL_2_BANK(slv_id,
  767. bank));
  768. }
  769. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  770. reg[len] = SWRM_CMD_FIFO_WR_CMD;
  771. val[len++] =
  772. SWR_REG_VAL_PACK(mport->lane_ctrl,
  773. port_req->dev_num, 0x00,
  774. SWRS_DP_LANE_CONTROL_BANK(slv_id,
  775. bank));
  776. }
  777. port_req->ch_en = port_req->req_ch;
  778. }
  779. value = ((mport->req_ch)
  780. << SWRM_DP_PORT_CTRL_EN_CHAN_SHFT);
  781. if (mport->offset2 != SWR_INVALID_PARAM)
  782. value |= ((mport->offset2)
  783. << SWRM_DP_PORT_CTRL_OFFSET2_SHFT);
  784. value |= ((mport->offset1)
  785. << SWRM_DP_PORT_CTRL_OFFSET1_SHFT);
  786. value |= mport->sinterval;
  787. reg[len] = SWRM_DP_PORT_CTRL_BANK(i + 1, bank);
  788. val[len++] = value;
  789. dev_dbg(swrm->dev, "%s: mport :%d, reg: 0x%x, val: 0x%x\n",
  790. __func__, i,
  791. (SWRM_DP_PORT_CTRL_BANK(i + 1, bank)), value);
  792. if (mport->lane_ctrl != SWR_INVALID_PARAM) {
  793. reg[len] = SWRM_DP_PORT_CTRL_2_BANK(i + 1, bank);
  794. val[len++] = mport->lane_ctrl;
  795. }
  796. if (mport->word_length != SWR_INVALID_PARAM) {
  797. reg[len] = SWRM_DP_BLOCK_CTRL_1(i + 1);
  798. val[len++] = mport->word_length;
  799. }
  800. if (mport->blk_grp_count != SWR_INVALID_PARAM) {
  801. reg[len] = SWRM_DP_BLOCK_CTRL2_BANK(i + 1, bank);
  802. val[len++] = mport->blk_grp_count;
  803. }
  804. if (mport->hstart != SWR_INVALID_PARAM
  805. && mport->hstop != SWR_INVALID_PARAM) {
  806. reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
  807. hparams = (mport->hstop << 4) | mport->hstart;
  808. val[len++] = hparams;
  809. } else {
  810. reg[len] = SWRM_DP_PORT_HCTRL_BANK(i + 1, bank);
  811. hparams = (SWR_HSTOP_MAX_VAL << 4) | SWR_HSTART_MIN_VAL;
  812. val[len++] = hparams;
  813. }
  814. if (mport->blk_pack_mode != SWR_INVALID_PARAM) {
  815. reg[len] = SWRM_DP_BLOCK_CTRL3_BANK(i + 1, bank);
  816. val[len++] = mport->blk_pack_mode;
  817. }
  818. mport->ch_en = mport->req_ch;
  819. }
  820. swr_master_bulk_write(swrm, reg, val, len);
  821. }
  822. static void swrm_apply_port_config(struct swr_master *master)
  823. {
  824. u8 bank;
  825. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  826. if (!swrm) {
  827. pr_err("%s: Invalid handle to swr controller\n",
  828. __func__);
  829. return;
  830. }
  831. bank = get_inactive_bank_num(swrm);
  832. dev_dbg(swrm->dev, "%s: enter bank: %d master_ports: %d\n",
  833. __func__, bank, master->num_port);
  834. swrm_cmd_fifo_wr_cmd(swrm, 0x01, 0xF, 0x00,
  835. SWRS_SCP_HOST_CLK_DIV2_CTL_BANK(bank));
  836. swrm_copy_data_port_config(master, bank);
  837. }
  838. static int swrm_slvdev_datapath_control(struct swr_master *master, bool enable)
  839. {
  840. u8 bank;
  841. u32 value, n_row, n_col;
  842. int ret;
  843. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  844. int mask = (SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_BMSK |
  845. SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_BMSK |
  846. SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_BMSK);
  847. u8 inactive_bank;
  848. if (!swrm) {
  849. pr_err("%s: swrm is null\n", __func__);
  850. return -EFAULT;
  851. }
  852. mutex_lock(&swrm->mlock);
  853. bank = get_inactive_bank_num(swrm);
  854. if (enable) {
  855. if (!test_bit(ENABLE_PENDING, &swrm->port_req_pending)) {
  856. dev_dbg(swrm->dev, "%s:No pending connect port req\n",
  857. __func__);
  858. goto exit;
  859. }
  860. clear_bit(ENABLE_PENDING, &swrm->port_req_pending);
  861. ret = swrm_get_port_config(swrm);
  862. if (ret) {
  863. /* cannot accommodate ports */
  864. swrm_cleanup_disabled_port_reqs(master);
  865. mutex_unlock(&swrm->mlock);
  866. return -EINVAL;
  867. }
  868. swr_master_write(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN,
  869. SWRM_INTERRUPT_STATUS_MASK);
  870. /* apply the new port config*/
  871. swrm_apply_port_config(master);
  872. } else {
  873. if (!test_bit(DISABLE_PENDING, &swrm->port_req_pending)) {
  874. dev_dbg(swrm->dev, "%s:No pending disconn port req\n",
  875. __func__);
  876. goto exit;
  877. }
  878. clear_bit(DISABLE_PENDING, &swrm->port_req_pending);
  879. swrm_disable_ports(master, bank);
  880. }
  881. dev_dbg(swrm->dev, "%s: enable: %d, cfg_devs: %d\n",
  882. __func__, enable, swrm->num_cfg_devs);
  883. if (enable) {
  884. /* set col = 16 */
  885. n_col = SWR_MAX_COL;
  886. } else {
  887. /*
  888. * Do not change to col = 2 if there are still active ports
  889. */
  890. if (!master->num_port)
  891. n_col = SWR_MIN_COL;
  892. else
  893. n_col = SWR_MAX_COL;
  894. }
  895. /* Use default 50 * x, frame shape. Change based on mclk */
  896. if (swrm->mclk_freq == MCLK_FREQ_NATIVE) {
  897. dev_dbg(swrm->dev, "setting 64 x %d frameshape\n",
  898. n_col ? 16 : 2);
  899. n_row = SWR_ROW_64;
  900. } else {
  901. dev_dbg(swrm->dev, "setting 50 x %d frameshape\n",
  902. n_col ? 16 : 2);
  903. n_row = SWR_ROW_50;
  904. }
  905. value = swr_master_read(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank));
  906. value &= (~mask);
  907. value |= ((n_row << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  908. (n_col << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  909. (0 << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  910. swr_master_write(swrm, SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  911. dev_dbg(swrm->dev, "%s: regaddr: 0x%x, value: 0x%x\n", __func__,
  912. SWRM_MCP_FRAME_CTRL_BANK_ADDR(bank), value);
  913. enable_bank_switch(swrm, bank, n_row, n_col);
  914. inactive_bank = bank ? 0 : 1;
  915. if (enable)
  916. swrm_copy_data_port_config(master, inactive_bank);
  917. else {
  918. swrm_disable_ports(master, inactive_bank);
  919. swrm_cleanup_disabled_port_reqs(master);
  920. }
  921. if (!swrm_is_port_en(master)) {
  922. dev_dbg(&master->dev, "%s: pm_runtime auto suspend triggered\n",
  923. __func__);
  924. pm_runtime_mark_last_busy(swrm->dev);
  925. pm_runtime_put_autosuspend(swrm->dev);
  926. }
  927. exit:
  928. mutex_unlock(&swrm->mlock);
  929. return 0;
  930. }
  931. static int swrm_connect_port(struct swr_master *master,
  932. struct swr_params *portinfo)
  933. {
  934. int i;
  935. struct swr_port_info *port_req;
  936. int ret = 0;
  937. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  938. struct swrm_mports *mport;
  939. u8 mstr_port_id, mstr_ch_msk;
  940. dev_dbg(&master->dev, "%s: enter\n", __func__);
  941. if (!portinfo)
  942. return -EINVAL;
  943. if (!swrm) {
  944. dev_err(&master->dev,
  945. "%s: Invalid handle to swr controller\n",
  946. __func__);
  947. return -EINVAL;
  948. }
  949. mutex_lock(&swrm->mlock);
  950. mutex_lock(&swrm->devlock);
  951. if (!swrm->dev_up) {
  952. mutex_unlock(&swrm->devlock);
  953. mutex_unlock(&swrm->mlock);
  954. return -EINVAL;
  955. }
  956. mutex_unlock(&swrm->devlock);
  957. if (!swrm_is_port_en(master))
  958. pm_runtime_get_sync(swrm->dev);
  959. for (i = 0; i < portinfo->num_port; i++) {
  960. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_msk,
  961. portinfo->port_type[i],
  962. portinfo->port_id[i]);
  963. if (ret) {
  964. dev_err(&master->dev,
  965. "%s: mstr portid for slv port %d not found\n",
  966. __func__, portinfo->port_id[i]);
  967. goto port_fail;
  968. }
  969. mport = &(swrm->mport_cfg[mstr_port_id]);
  970. /* get port req */
  971. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  972. portinfo->dev_num);
  973. if (!port_req) {
  974. dev_dbg(&master->dev, "%s: new req:port id %d dev %d\n",
  975. __func__, portinfo->port_id[i],
  976. portinfo->dev_num);
  977. port_req = kzalloc(sizeof(struct swr_port_info),
  978. GFP_KERNEL);
  979. if (!port_req) {
  980. ret = -ENOMEM;
  981. goto mem_fail;
  982. }
  983. port_req->dev_num = portinfo->dev_num;
  984. port_req->slave_port_id = portinfo->port_id[i];
  985. port_req->num_ch = portinfo->num_ch[i];
  986. port_req->ch_rate = portinfo->ch_rate[i];
  987. port_req->ch_en = 0;
  988. port_req->master_port_id = mstr_port_id;
  989. list_add(&port_req->list, &mport->port_req_list);
  990. }
  991. port_req->req_ch |= portinfo->ch_en[i];
  992. dev_dbg(&master->dev,
  993. "%s: mstr port %d, slv port %d ch_rate %d num_ch %d\n",
  994. __func__, port_req->master_port_id,
  995. port_req->slave_port_id, port_req->ch_rate,
  996. port_req->num_ch);
  997. /* Put the port req on master port */
  998. mport = &(swrm->mport_cfg[mstr_port_id]);
  999. mport->port_en = true;
  1000. mport->req_ch |= mstr_ch_msk;
  1001. master->port_en_mask |= (1 << mstr_port_id);
  1002. }
  1003. master->num_port += portinfo->num_port;
  1004. set_bit(ENABLE_PENDING, &swrm->port_req_pending);
  1005. swr_port_response(master, portinfo->tid);
  1006. mutex_unlock(&swrm->mlock);
  1007. return 0;
  1008. port_fail:
  1009. mem_fail:
  1010. /* cleanup port reqs in error condition */
  1011. swrm_cleanup_disabled_port_reqs(master);
  1012. mutex_unlock(&swrm->mlock);
  1013. return ret;
  1014. }
  1015. static int swrm_disconnect_port(struct swr_master *master,
  1016. struct swr_params *portinfo)
  1017. {
  1018. int i, ret = 0;
  1019. struct swr_port_info *port_req;
  1020. struct swrm_mports *mport;
  1021. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(master);
  1022. u8 mstr_port_id, mstr_ch_mask;
  1023. if (!swrm) {
  1024. dev_err(&master->dev,
  1025. "%s: Invalid handle to swr controller\n",
  1026. __func__);
  1027. return -EINVAL;
  1028. }
  1029. if (!portinfo) {
  1030. dev_err(&master->dev, "%s: portinfo is NULL\n", __func__);
  1031. return -EINVAL;
  1032. }
  1033. mutex_lock(&swrm->mlock);
  1034. for (i = 0; i < portinfo->num_port; i++) {
  1035. ret = swrm_get_master_port(swrm, &mstr_port_id, &mstr_ch_mask,
  1036. portinfo->port_type[i], portinfo->port_id[i]);
  1037. if (ret) {
  1038. dev_err(&master->dev,
  1039. "%s: mstr portid for slv port %d not found\n",
  1040. __func__, portinfo->port_id[i]);
  1041. mutex_unlock(&swrm->mlock);
  1042. return -EINVAL;
  1043. }
  1044. mport = &(swrm->mport_cfg[mstr_port_id]);
  1045. /* get port req */
  1046. port_req = swrm_get_port_req(mport, portinfo->port_id[i],
  1047. portinfo->dev_num);
  1048. if (!port_req) {
  1049. dev_err(&master->dev, "%s:port not enabled : port %d\n",
  1050. __func__, portinfo->port_id[i]);
  1051. mutex_unlock(&swrm->mlock);
  1052. return -EINVAL;
  1053. }
  1054. port_req->req_ch &= ~portinfo->ch_en[i];
  1055. mport->req_ch &= ~mstr_ch_mask;
  1056. }
  1057. master->num_port -= portinfo->num_port;
  1058. set_bit(DISABLE_PENDING, &swrm->port_req_pending);
  1059. swr_port_response(master, portinfo->tid);
  1060. mutex_unlock(&swrm->mlock);
  1061. return 0;
  1062. }
  1063. static int swrm_find_alert_slave(struct swr_mstr_ctrl *swrm,
  1064. int status, u8 *devnum)
  1065. {
  1066. int i;
  1067. bool found = false;
  1068. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1069. if ((status & SWRM_MCP_SLV_STATUS_MASK) == SWR_ALERT) {
  1070. *devnum = i;
  1071. found = true;
  1072. break;
  1073. }
  1074. status >>= 2;
  1075. }
  1076. if (found)
  1077. return 0;
  1078. else
  1079. return -EINVAL;
  1080. }
  1081. static int swrm_check_slave_change_status(struct swr_mstr_ctrl *swrm,
  1082. int status, u8 *devnum)
  1083. {
  1084. int i;
  1085. int new_sts = status;
  1086. int ret = SWR_NOT_PRESENT;
  1087. if (status != swrm->slave_status) {
  1088. for (i = 0; i < (swrm->master.num_dev + 1); i++) {
  1089. if ((status & SWRM_MCP_SLV_STATUS_MASK) !=
  1090. (swrm->slave_status & SWRM_MCP_SLV_STATUS_MASK)) {
  1091. ret = (status & SWRM_MCP_SLV_STATUS_MASK);
  1092. *devnum = i;
  1093. break;
  1094. }
  1095. status >>= 2;
  1096. swrm->slave_status >>= 2;
  1097. }
  1098. swrm->slave_status = new_sts;
  1099. }
  1100. return ret;
  1101. }
  1102. static irqreturn_t swr_mstr_interrupt(int irq, void *dev)
  1103. {
  1104. struct swr_mstr_ctrl *swrm = dev;
  1105. u32 value, intr_sts, intr_mask;
  1106. u32 temp = 0;
  1107. u32 status, chg_sts, i;
  1108. u8 devnum = 0;
  1109. int ret = IRQ_HANDLED;
  1110. struct swr_device *swr_dev;
  1111. struct swr_master *mstr = &swrm->master;
  1112. mutex_lock(&swrm->reslock);
  1113. swrm_clk_request(swrm, true);
  1114. mutex_unlock(&swrm->reslock);
  1115. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1116. intr_mask = swr_master_read(swrm, SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN);
  1117. intr_sts &= intr_mask;
  1118. handle_irq:
  1119. for (i = 0; i < SWRM_INTERRUPT_MAX; i++) {
  1120. value = intr_sts & (1 << i);
  1121. if (!value)
  1122. continue;
  1123. switch (value) {
  1124. case SWRM_INTERRUPT_STATUS_SLAVE_PEND_IRQ:
  1125. dev_dbg(swrm->dev, "Trigger irq to slave device\n");
  1126. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1127. ret = swrm_find_alert_slave(swrm, status, &devnum);
  1128. if (ret) {
  1129. dev_err_ratelimited(swrm->dev,
  1130. "no slave alert found.spurious interrupt\n");
  1131. break;
  1132. }
  1133. swrm_cmd_fifo_rd_cmd(swrm, &temp, devnum, 0x0,
  1134. SWRS_SCP_INT_STATUS_CLEAR_1, 1);
  1135. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1136. SWRS_SCP_INT_STATUS_CLEAR_1);
  1137. swrm_cmd_fifo_wr_cmd(swrm, 0x0, devnum, 0x0,
  1138. SWRS_SCP_INT_STATUS_CLEAR_1);
  1139. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1140. if (swr_dev->dev_num != devnum)
  1141. continue;
  1142. if (swr_dev->slave_irq) {
  1143. do {
  1144. handle_nested_irq(
  1145. irq_find_mapping(
  1146. swr_dev->slave_irq, 0));
  1147. } while (swr_dev->slave_irq_pending);
  1148. }
  1149. }
  1150. break;
  1151. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_ATTACHED:
  1152. dev_dbg(swrm->dev, "SWR new slave attached\n");
  1153. break;
  1154. case SWRM_INTERRUPT_STATUS_CHANGE_ENUM_SLAVE_STATUS:
  1155. status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1156. if (status == swrm->slave_status) {
  1157. dev_dbg(swrm->dev,
  1158. "%s: No change in slave status: %d\n",
  1159. __func__, status);
  1160. break;
  1161. }
  1162. chg_sts = swrm_check_slave_change_status(swrm, status,
  1163. &devnum);
  1164. switch (chg_sts) {
  1165. case SWR_NOT_PRESENT:
  1166. dev_dbg(swrm->dev, "device %d got detached\n",
  1167. devnum);
  1168. break;
  1169. case SWR_ATTACHED_OK:
  1170. dev_dbg(swrm->dev, "device %d got attached\n",
  1171. devnum);
  1172. /* enable host irq from slave device*/
  1173. swrm_cmd_fifo_wr_cmd(swrm, 0xFF, devnum, 0x0,
  1174. SWRS_SCP_INT_STATUS_CLEAR_1);
  1175. swrm_cmd_fifo_wr_cmd(swrm, 0x4, devnum, 0x0,
  1176. SWRS_SCP_INT_STATUS_MASK_1);
  1177. break;
  1178. case SWR_ALERT:
  1179. dev_dbg(swrm->dev,
  1180. "device %d has pending interrupt\n",
  1181. devnum);
  1182. break;
  1183. }
  1184. break;
  1185. case SWRM_INTERRUPT_STATUS_MASTER_CLASH_DET:
  1186. dev_err_ratelimited(swrm->dev,
  1187. "SWR bus clsh detected\n");
  1188. break;
  1189. case SWRM_INTERRUPT_STATUS_RD_FIFO_OVERFLOW:
  1190. dev_dbg(swrm->dev, "SWR read FIFO overflow\n");
  1191. break;
  1192. case SWRM_INTERRUPT_STATUS_RD_FIFO_UNDERFLOW:
  1193. dev_dbg(swrm->dev, "SWR read FIFO underflow\n");
  1194. break;
  1195. case SWRM_INTERRUPT_STATUS_WR_CMD_FIFO_OVERFLOW:
  1196. dev_dbg(swrm->dev, "SWR write FIFO overflow\n");
  1197. break;
  1198. case SWRM_INTERRUPT_STATUS_CMD_ERROR:
  1199. value = swr_master_read(swrm, SWRM_CMD_FIFO_STATUS);
  1200. dev_err_ratelimited(swrm->dev,
  1201. "SWR CMD error, fifo status 0x%x, flushing fifo\n",
  1202. value);
  1203. swr_master_write(swrm, SWRM_CMD_FIFO_CMD, 0x1);
  1204. break;
  1205. case SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION:
  1206. dev_err_ratelimited(swrm->dev, "SWR Port collision detected\n");
  1207. intr_mask &= ~SWRM_INTERRUPT_STATUS_DOUT_PORT_COLLISION;
  1208. swr_master_write(swrm,
  1209. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, intr_mask);
  1210. break;
  1211. case SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH:
  1212. dev_dbg(swrm->dev, "SWR read enable valid mismatch\n");
  1213. intr_mask &=
  1214. ~SWRM_INTERRUPT_STATUS_READ_EN_RD_VALID_MISMATCH;
  1215. swr_master_write(swrm,
  1216. SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN, intr_mask);
  1217. break;
  1218. case SWRM_INTERRUPT_STATUS_SPECIAL_CMD_ID_FINISHED:
  1219. complete(&swrm->broadcast);
  1220. dev_dbg(swrm->dev, "SWR cmd id finished\n");
  1221. break;
  1222. case SWRM_INTERRUPT_STATUS_NEW_SLAVE_AUTO_ENUM_FINISHED:
  1223. break;
  1224. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_FAILED:
  1225. break;
  1226. case SWRM_INTERRUPT_STATUS_AUTO_ENUM_TABLE_IS_FULL:
  1227. break;
  1228. case SWRM_INTERRUPT_STATUS_BUS_RESET_FINISHED:
  1229. complete(&swrm->reset);
  1230. break;
  1231. case SWRM_INTERRUPT_STATUS_CLK_STOP_FINISHED:
  1232. break;
  1233. default:
  1234. dev_err_ratelimited(swrm->dev,
  1235. "SWR unknown interrupt\n");
  1236. ret = IRQ_NONE;
  1237. break;
  1238. }
  1239. }
  1240. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, intr_sts);
  1241. swr_master_write(swrm, SWRM_INTERRUPT_CLEAR, 0x0);
  1242. intr_sts = swr_master_read(swrm, SWRM_INTERRUPT_STATUS);
  1243. intr_sts &= intr_mask;
  1244. if (intr_sts) {
  1245. dev_dbg(swrm->dev, "%s: new interrupt received\n", __func__);
  1246. goto handle_irq;
  1247. }
  1248. mutex_lock(&swrm->reslock);
  1249. swrm_clk_request(swrm, false);
  1250. mutex_unlock(&swrm->reslock);
  1251. return ret;
  1252. }
  1253. static irqreturn_t swrm_wakeup_interrupt(int irq, void *dev)
  1254. {
  1255. struct swr_mstr_ctrl *swrm = dev;
  1256. int ret = IRQ_HANDLED;
  1257. if (!swrm || !(swrm->dev)) {
  1258. pr_err("%s: swrm or dev is null\n", __func__);
  1259. return IRQ_NONE;
  1260. }
  1261. mutex_lock(&swrm->devlock);
  1262. if (!swrm->dev_up) {
  1263. if (swrm->wake_irq > 0)
  1264. disable_irq_nosync(swrm->wake_irq);
  1265. mutex_unlock(&swrm->devlock);
  1266. return ret;
  1267. }
  1268. mutex_unlock(&swrm->devlock);
  1269. if (swrm->wake_irq > 0)
  1270. disable_irq_nosync(swrm->wake_irq);
  1271. pm_runtime_get_sync(swrm->dev);
  1272. pm_runtime_mark_last_busy(swrm->dev);
  1273. pm_runtime_put_autosuspend(swrm->dev);
  1274. return ret;
  1275. }
  1276. static void swrm_wakeup_work(struct work_struct *work)
  1277. {
  1278. struct swr_mstr_ctrl *swrm;
  1279. swrm = container_of(work, struct swr_mstr_ctrl,
  1280. wakeup_work);
  1281. if (!swrm || !(swrm->dev)) {
  1282. pr_err("%s: swrm or dev is null\n", __func__);
  1283. return;
  1284. }
  1285. mutex_lock(&swrm->devlock);
  1286. if (!swrm->dev_up) {
  1287. mutex_unlock(&swrm->devlock);
  1288. return;
  1289. }
  1290. mutex_unlock(&swrm->devlock);
  1291. pm_runtime_get_sync(swrm->dev);
  1292. pm_runtime_mark_last_busy(swrm->dev);
  1293. pm_runtime_put_autosuspend(swrm->dev);
  1294. }
  1295. static int swrm_get_device_status(struct swr_mstr_ctrl *swrm, u8 devnum)
  1296. {
  1297. u32 val;
  1298. swrm->slave_status = swr_master_read(swrm, SWRM_MCP_SLV_STATUS);
  1299. val = (swrm->slave_status >> (devnum * 2));
  1300. val &= SWRM_MCP_SLV_STATUS_MASK;
  1301. return val;
  1302. }
  1303. static int swrm_get_logical_dev_num(struct swr_master *mstr, u64 dev_id,
  1304. u8 *dev_num)
  1305. {
  1306. int i;
  1307. u64 id = 0;
  1308. int ret = -EINVAL;
  1309. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1310. struct swr_device *swr_dev;
  1311. u32 num_dev = 0;
  1312. if (!swrm) {
  1313. pr_err("%s: Invalid handle to swr controller\n",
  1314. __func__);
  1315. return ret;
  1316. }
  1317. if (swrm->num_dev)
  1318. num_dev = swrm->num_dev;
  1319. else
  1320. num_dev = mstr->num_dev;
  1321. mutex_lock(&swrm->devlock);
  1322. if (!swrm->dev_up) {
  1323. mutex_unlock(&swrm->devlock);
  1324. return ret;
  1325. }
  1326. mutex_unlock(&swrm->devlock);
  1327. pm_runtime_get_sync(swrm->dev);
  1328. for (i = 1; i < (num_dev + 1); i++) {
  1329. id = ((u64)(swr_master_read(swrm,
  1330. SWRM_ENUMERATOR_SLAVE_DEV_ID_2(i))) << 32);
  1331. id |= swr_master_read(swrm,
  1332. SWRM_ENUMERATOR_SLAVE_DEV_ID_1(i));
  1333. /*
  1334. * As pm_runtime_get_sync() brings all slaves out of reset
  1335. * update logical device number for all slaves.
  1336. */
  1337. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1338. if (swr_dev->addr == (id & SWR_DEV_ID_MASK)) {
  1339. u32 status = swrm_get_device_status(swrm, i);
  1340. if ((status == 0x01) || (status == 0x02)) {
  1341. swr_dev->dev_num = i;
  1342. if ((id & SWR_DEV_ID_MASK) == dev_id) {
  1343. *dev_num = i;
  1344. ret = 0;
  1345. }
  1346. dev_dbg(swrm->dev,
  1347. "%s: devnum %d is assigned for dev addr %lx\n",
  1348. __func__, i, swr_dev->addr);
  1349. }
  1350. }
  1351. }
  1352. }
  1353. if (ret)
  1354. dev_err(swrm->dev, "%s: device 0x%llx is not ready\n",
  1355. __func__, dev_id);
  1356. pm_runtime_mark_last_busy(swrm->dev);
  1357. pm_runtime_put_autosuspend(swrm->dev);
  1358. return ret;
  1359. }
  1360. static void swrm_device_wakeup_vote(struct swr_master *mstr)
  1361. {
  1362. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1363. if (!swrm) {
  1364. pr_err("%s: Invalid handle to swr controller\n",
  1365. __func__);
  1366. return;
  1367. }
  1368. pm_runtime_get_sync(swrm->dev);
  1369. }
  1370. static void swrm_device_wakeup_unvote(struct swr_master *mstr)
  1371. {
  1372. struct swr_mstr_ctrl *swrm = swr_get_ctrl_data(mstr);
  1373. if (!swrm) {
  1374. pr_err("%s: Invalid handle to swr controller\n",
  1375. __func__);
  1376. return;
  1377. }
  1378. pm_runtime_mark_last_busy(swrm->dev);
  1379. pm_runtime_put_autosuspend(swrm->dev);
  1380. }
  1381. static int swrm_master_init(struct swr_mstr_ctrl *swrm)
  1382. {
  1383. int ret = 0;
  1384. u32 val;
  1385. u8 row_ctrl = SWR_ROW_50;
  1386. u8 col_ctrl = SWR_MIN_COL;
  1387. u8 ssp_period = 1;
  1388. u8 retry_cmd_num = 3;
  1389. u32 reg[SWRM_MAX_INIT_REG];
  1390. u32 value[SWRM_MAX_INIT_REG];
  1391. int len = 0;
  1392. /* Clear Rows and Cols */
  1393. val = ((row_ctrl << SWRM_MCP_FRAME_CTRL_BANK_ROW_CTRL_SHFT) |
  1394. (col_ctrl << SWRM_MCP_FRAME_CTRL_BANK_COL_CTRL_SHFT) |
  1395. (ssp_period << SWRM_MCP_FRAME_CTRL_BANK_SSP_PERIOD_SHFT));
  1396. reg[len] = SWRM_MCP_FRAME_CTRL_BANK_ADDR(0);
  1397. value[len++] = val;
  1398. /* Set Auto enumeration flag */
  1399. reg[len] = SWRM_ENUMERATOR_CFG_ADDR;
  1400. value[len++] = 1;
  1401. /* Configure No pings */
  1402. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1403. val &= ~SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_BMSK;
  1404. val |= (0x1f << SWRM_MCP_CFG_MAX_NUM_OF_CMD_NO_PINGS_SHFT);
  1405. reg[len] = SWRM_MCP_CFG_ADDR;
  1406. value[len++] = val;
  1407. /* Configure number of retries of a read/write cmd */
  1408. val = (retry_cmd_num << SWRM_CMD_FIFO_CFG_NUM_OF_CMD_RETRY_SHFT);
  1409. reg[len] = SWRM_CMD_FIFO_CFG_ADDR;
  1410. value[len++] = val;
  1411. reg[len] = SWRM_MCP_BUS_CTRL_ADDR;
  1412. value[len++] = 0x2;
  1413. /* Set IRQ to PULSE */
  1414. reg[len] = SWRM_COMP_CFG_ADDR;
  1415. value[len++] = 0x02;
  1416. reg[len] = SWRM_COMP_CFG_ADDR;
  1417. value[len++] = 0x03;
  1418. reg[len] = SWRM_INTERRUPT_CLEAR;
  1419. value[len++] = 0xFFFFFFFF;
  1420. /* Mask soundwire interrupts */
  1421. reg[len] = SWRM_INTERRUPT_MASK_ADDR;
  1422. value[len++] = 0x1FFFD;
  1423. reg[len] = SWR_MSTR_RX_SWRM_CPU_INTERRUPT_EN;
  1424. value[len++] = SWRM_INTERRUPT_STATUS_MASK;
  1425. swr_master_bulk_write(swrm, reg, value, len);
  1426. return ret;
  1427. }
  1428. static int swrm_event_notify(struct notifier_block *self,
  1429. unsigned long action, void *data)
  1430. {
  1431. struct swr_mstr_ctrl *swrm = container_of(self, struct swr_mstr_ctrl,
  1432. event_notifier);
  1433. if (!swrm || !(swrm->dev)) {
  1434. pr_err("%s: swrm or dev is NULL\n", __func__);
  1435. return -EINVAL;
  1436. }
  1437. switch (action) {
  1438. case MSM_AUD_DC_EVENT:
  1439. schedule_work(&(swrm->dc_presence_work));
  1440. break;
  1441. case SWR_WAKE_IRQ_EVENT:
  1442. if (swrm->ipc_wakeup && !swrm->ipc_wakeup_triggered) {
  1443. swrm->ipc_wakeup_triggered = true;
  1444. schedule_work(&swrm->wakeup_work);
  1445. }
  1446. break;
  1447. default:
  1448. dev_err(swrm->dev, "%s: invalid event type: %lu\n",
  1449. __func__, action);
  1450. return -EINVAL;
  1451. }
  1452. return 0;
  1453. }
  1454. static void swrm_notify_work_fn(struct work_struct *work)
  1455. {
  1456. struct swr_mstr_ctrl *swrm = container_of(work, struct swr_mstr_ctrl,
  1457. dc_presence_work);
  1458. if (!swrm || !swrm->pdev) {
  1459. pr_err("%s: swrm or pdev is NULL\n", __func__);
  1460. return;
  1461. }
  1462. swrm_wcd_notify(swrm->pdev, SWR_DEVICE_DOWN, NULL);
  1463. }
  1464. static int swrm_probe(struct platform_device *pdev)
  1465. {
  1466. struct swr_mstr_ctrl *swrm;
  1467. struct swr_ctrl_platform_data *pdata;
  1468. u32 i, num_ports, port_num, port_type, ch_mask;
  1469. u32 *temp, map_size, map_length, ch_iter = 0, old_port_num = 0;
  1470. int ret = 0;
  1471. /* Allocate soundwire master driver structure */
  1472. swrm = devm_kzalloc(&pdev->dev, sizeof(struct swr_mstr_ctrl),
  1473. GFP_KERNEL);
  1474. if (!swrm) {
  1475. ret = -ENOMEM;
  1476. goto err_memory_fail;
  1477. }
  1478. swrm->pdev = pdev;
  1479. swrm->dev = &pdev->dev;
  1480. platform_set_drvdata(pdev, swrm);
  1481. swr_set_ctrl_data(&swrm->master, swrm);
  1482. pdata = dev_get_platdata(&pdev->dev);
  1483. if (!pdata) {
  1484. dev_err(&pdev->dev, "%s: pdata from parent is NULL\n",
  1485. __func__);
  1486. ret = -EINVAL;
  1487. goto err_pdata_fail;
  1488. }
  1489. swrm->handle = (void *)pdata->handle;
  1490. if (!swrm->handle) {
  1491. dev_err(&pdev->dev, "%s: swrm->handle is NULL\n",
  1492. __func__);
  1493. ret = -EINVAL;
  1494. goto err_pdata_fail;
  1495. }
  1496. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr_master_id",
  1497. &swrm->master_id);
  1498. if (ret) {
  1499. dev_err(&pdev->dev, "%s: failed to get master id\n", __func__);
  1500. goto err_pdata_fail;
  1501. }
  1502. if (!(of_property_read_u32(pdev->dev.of_node,
  1503. "swrm-io-base", &swrm->swrm_base_reg)))
  1504. ret = of_property_read_u32(pdev->dev.of_node,
  1505. "swrm-io-base", &swrm->swrm_base_reg);
  1506. if (!swrm->swrm_base_reg) {
  1507. swrm->read = pdata->read;
  1508. if (!swrm->read) {
  1509. dev_err(&pdev->dev, "%s: swrm->read is NULL\n",
  1510. __func__);
  1511. ret = -EINVAL;
  1512. goto err_pdata_fail;
  1513. }
  1514. swrm->write = pdata->write;
  1515. if (!swrm->write) {
  1516. dev_err(&pdev->dev, "%s: swrm->write is NULL\n",
  1517. __func__);
  1518. ret = -EINVAL;
  1519. goto err_pdata_fail;
  1520. }
  1521. swrm->bulk_write = pdata->bulk_write;
  1522. if (!swrm->bulk_write) {
  1523. dev_err(&pdev->dev, "%s: swrm->bulk_write is NULL\n",
  1524. __func__);
  1525. ret = -EINVAL;
  1526. goto err_pdata_fail;
  1527. }
  1528. } else {
  1529. swrm->swrm_dig_base = devm_ioremap(&pdev->dev,
  1530. swrm->swrm_base_reg, SWRM_MAX_REGISTER);
  1531. }
  1532. swrm->clk = pdata->clk;
  1533. if (!swrm->clk) {
  1534. dev_err(&pdev->dev, "%s: swrm->clk is NULL\n",
  1535. __func__);
  1536. ret = -EINVAL;
  1537. goto err_pdata_fail;
  1538. }
  1539. if (of_property_read_u32(pdev->dev.of_node,
  1540. "qcom,swr-clock-stop-mode0",
  1541. &swrm->clk_stop_mode0_supp)) {
  1542. swrm->clk_stop_mode0_supp = FALSE;
  1543. }
  1544. /* Parse soundwire port mapping */
  1545. ret = of_property_read_u32(pdev->dev.of_node, "qcom,swr-num-ports",
  1546. &num_ports);
  1547. if (ret) {
  1548. dev_err(swrm->dev, "%s: Failed to get num_ports\n", __func__);
  1549. goto err_pdata_fail;
  1550. }
  1551. swrm->num_ports = num_ports;
  1552. if (!of_find_property(pdev->dev.of_node, "qcom,swr-port-mapping",
  1553. &map_size)) {
  1554. dev_err(swrm->dev, "missing port mapping\n");
  1555. goto err_pdata_fail;
  1556. }
  1557. map_length = map_size / (3 * sizeof(u32));
  1558. if (num_ports > SWR_MSTR_PORT_LEN) {
  1559. dev_err(&pdev->dev, "%s:invalid number of swr ports\n",
  1560. __func__);
  1561. ret = -EINVAL;
  1562. goto err_pdata_fail;
  1563. }
  1564. temp = devm_kzalloc(&pdev->dev, map_size, GFP_KERNEL);
  1565. if (!temp) {
  1566. ret = -ENOMEM;
  1567. goto err_pdata_fail;
  1568. }
  1569. ret = of_property_read_u32_array(pdev->dev.of_node,
  1570. "qcom,swr-port-mapping", temp, 3 * map_length);
  1571. if (ret) {
  1572. dev_err(swrm->dev, "%s: Failed to read port mapping\n",
  1573. __func__);
  1574. goto err_pdata_fail;
  1575. }
  1576. for (i = 0; i < map_length; i++) {
  1577. port_num = temp[3 * i];
  1578. port_type = temp[3 * i + 1];
  1579. ch_mask = temp[3 * i + 2];
  1580. if (port_num != old_port_num)
  1581. ch_iter = 0;
  1582. swrm->port_mapping[port_num][ch_iter].port_type = port_type;
  1583. swrm->port_mapping[port_num][ch_iter++].ch_mask = ch_mask;
  1584. old_port_num = port_num;
  1585. }
  1586. devm_kfree(&pdev->dev, temp);
  1587. swrm->reg_irq = pdata->reg_irq;
  1588. swrm->master.read = swrm_read;
  1589. swrm->master.write = swrm_write;
  1590. swrm->master.bulk_write = swrm_bulk_write;
  1591. swrm->master.get_logical_dev_num = swrm_get_logical_dev_num;
  1592. swrm->master.connect_port = swrm_connect_port;
  1593. swrm->master.disconnect_port = swrm_disconnect_port;
  1594. swrm->master.slvdev_datapath_control = swrm_slvdev_datapath_control;
  1595. swrm->master.remove_from_group = swrm_remove_from_group;
  1596. swrm->master.device_wakeup_vote = swrm_device_wakeup_vote;
  1597. swrm->master.device_wakeup_unvote = swrm_device_wakeup_unvote;
  1598. swrm->master.dev.parent = &pdev->dev;
  1599. swrm->master.dev.of_node = pdev->dev.of_node;
  1600. swrm->master.num_port = 0;
  1601. swrm->rcmd_id = 0;
  1602. swrm->wcmd_id = 0;
  1603. swrm->slave_status = 0;
  1604. swrm->num_rx_chs = 0;
  1605. swrm->clk_ref_count = 0;
  1606. swrm->mclk_freq = MCLK_FREQ;
  1607. swrm->dev_up = true;
  1608. swrm->state = SWR_MSTR_UP;
  1609. swrm->ipc_wakeup = false;
  1610. swrm->ipc_wakeup_triggered = false;
  1611. init_completion(&swrm->reset);
  1612. init_completion(&swrm->broadcast);
  1613. init_completion(&swrm->clk_off_complete);
  1614. mutex_init(&swrm->mlock);
  1615. mutex_init(&swrm->reslock);
  1616. mutex_init(&swrm->force_down_lock);
  1617. mutex_init(&swrm->iolock);
  1618. mutex_init(&swrm->clklock);
  1619. mutex_init(&swrm->devlock);
  1620. for (i = 0 ; i < SWR_MSTR_PORT_LEN; i++)
  1621. INIT_LIST_HEAD(&swrm->mport_cfg[i].port_req_list);
  1622. ret = of_property_read_u32(swrm->dev->of_node, "qcom,swr-num-dev",
  1623. &swrm->num_dev);
  1624. if (ret) {
  1625. dev_dbg(&pdev->dev, "%s: Looking up %s property failed\n",
  1626. __func__, "qcom,swr-num-dev");
  1627. } else {
  1628. if (swrm->num_dev > SWR_MAX_SLAVE_DEVICES) {
  1629. dev_err(&pdev->dev, "%s: num_dev %d > max limit %d\n",
  1630. __func__, swrm->num_dev, SWR_MAX_SLAVE_DEVICES);
  1631. ret = -EINVAL;
  1632. goto err_pdata_fail;
  1633. }
  1634. }
  1635. if (swrm->reg_irq) {
  1636. ret = swrm->reg_irq(swrm->handle, swr_mstr_interrupt, swrm,
  1637. SWR_IRQ_REGISTER);
  1638. if (ret) {
  1639. dev_err(&pdev->dev, "%s: IRQ register failed ret %d\n",
  1640. __func__, ret);
  1641. goto err_irq_fail;
  1642. }
  1643. } else {
  1644. swrm->irq = platform_get_irq_byname(pdev, "swr_master_irq");
  1645. if (swrm->irq < 0) {
  1646. dev_err(swrm->dev, "%s() error getting irq hdle: %d\n",
  1647. __func__, swrm->irq);
  1648. goto err_irq_fail;
  1649. }
  1650. ret = request_threaded_irq(swrm->irq, NULL,
  1651. swr_mstr_interrupt,
  1652. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1653. "swr_master_irq", swrm);
  1654. if (ret) {
  1655. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1656. __func__, ret);
  1657. goto err_irq_fail;
  1658. }
  1659. }
  1660. ret = swr_register_master(&swrm->master);
  1661. if (ret) {
  1662. dev_err(&pdev->dev, "%s: error adding swr master\n", __func__);
  1663. goto err_mstr_fail;
  1664. }
  1665. /* Add devices registered with board-info as the
  1666. * controller will be up now
  1667. */
  1668. swr_master_add_boarddevices(&swrm->master);
  1669. mutex_lock(&swrm->mlock);
  1670. swrm_clk_request(swrm, true);
  1671. ret = swrm_master_init(swrm);
  1672. if (ret < 0) {
  1673. dev_err(&pdev->dev,
  1674. "%s: Error in master Initialization , err %d\n",
  1675. __func__, ret);
  1676. mutex_unlock(&swrm->mlock);
  1677. goto err_mstr_fail;
  1678. }
  1679. swrm->version = swr_master_read(swrm, SWRM_COMP_HW_VERSION);
  1680. mutex_unlock(&swrm->mlock);
  1681. INIT_WORK(&swrm->wakeup_work, swrm_wakeup_work);
  1682. if (pdev->dev.of_node)
  1683. of_register_swr_devices(&swrm->master);
  1684. dbgswrm = swrm;
  1685. debugfs_swrm_dent = debugfs_create_dir(dev_name(&pdev->dev), 0);
  1686. if (!IS_ERR(debugfs_swrm_dent)) {
  1687. debugfs_peek = debugfs_create_file("swrm_peek",
  1688. S_IFREG | 0444, debugfs_swrm_dent,
  1689. (void *) "swrm_peek", &swrm_debug_ops);
  1690. debugfs_poke = debugfs_create_file("swrm_poke",
  1691. S_IFREG | 0444, debugfs_swrm_dent,
  1692. (void *) "swrm_poke", &swrm_debug_ops);
  1693. debugfs_reg_dump = debugfs_create_file("swrm_reg_dump",
  1694. S_IFREG | 0444, debugfs_swrm_dent,
  1695. (void *) "swrm_reg_dump",
  1696. &swrm_debug_ops);
  1697. }
  1698. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1699. pm_runtime_use_autosuspend(&pdev->dev);
  1700. pm_runtime_set_active(&pdev->dev);
  1701. pm_runtime_enable(&pdev->dev);
  1702. pm_runtime_mark_last_busy(&pdev->dev);
  1703. INIT_WORK(&swrm->dc_presence_work, swrm_notify_work_fn);
  1704. swrm->event_notifier.notifier_call = swrm_event_notify;
  1705. msm_aud_evt_register_client(&swrm->event_notifier);
  1706. return 0;
  1707. err_mstr_fail:
  1708. if (swrm->reg_irq)
  1709. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1710. swrm, SWR_IRQ_FREE);
  1711. else if (swrm->irq)
  1712. free_irq(swrm->irq, swrm);
  1713. err_irq_fail:
  1714. mutex_destroy(&swrm->mlock);
  1715. mutex_destroy(&swrm->reslock);
  1716. mutex_destroy(&swrm->force_down_lock);
  1717. mutex_destroy(&swrm->iolock);
  1718. mutex_destroy(&swrm->clklock);
  1719. err_pdata_fail:
  1720. err_memory_fail:
  1721. return ret;
  1722. }
  1723. static int swrm_remove(struct platform_device *pdev)
  1724. {
  1725. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1726. if (swrm->reg_irq)
  1727. swrm->reg_irq(swrm->handle, swr_mstr_interrupt,
  1728. swrm, SWR_IRQ_FREE);
  1729. else if (swrm->irq)
  1730. free_irq(swrm->irq, swrm);
  1731. else if (swrm->wake_irq > 0)
  1732. free_irq(swrm->wake_irq, swrm);
  1733. pm_runtime_disable(&pdev->dev);
  1734. pm_runtime_set_suspended(&pdev->dev);
  1735. swr_unregister_master(&swrm->master);
  1736. msm_aud_evt_unregister_client(&swrm->event_notifier);
  1737. mutex_destroy(&swrm->mlock);
  1738. mutex_destroy(&swrm->reslock);
  1739. mutex_destroy(&swrm->iolock);
  1740. mutex_destroy(&swrm->clklock);
  1741. mutex_destroy(&swrm->force_down_lock);
  1742. devm_kfree(&pdev->dev, swrm);
  1743. return 0;
  1744. }
  1745. static int swrm_clk_pause(struct swr_mstr_ctrl *swrm)
  1746. {
  1747. u32 val;
  1748. dev_dbg(swrm->dev, "%s: state: %d\n", __func__, swrm->state);
  1749. swr_master_write(swrm, SWRM_INTERRUPT_MASK_ADDR, 0x1FDFD);
  1750. val = swr_master_read(swrm, SWRM_MCP_CFG_ADDR);
  1751. val |= SWRM_MCP_CFG_BUS_CLK_PAUSE_BMSK;
  1752. swr_master_write(swrm, SWRM_MCP_CFG_ADDR, val);
  1753. return 0;
  1754. }
  1755. #ifdef CONFIG_PM
  1756. static int swrm_runtime_resume(struct device *dev)
  1757. {
  1758. struct platform_device *pdev = to_platform_device(dev);
  1759. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1760. int ret = 0;
  1761. struct swr_master *mstr = &swrm->master;
  1762. struct swr_device *swr_dev;
  1763. dev_dbg(dev, "%s: pm_runtime: resume, state:%d\n",
  1764. __func__, swrm->state);
  1765. mutex_lock(&swrm->reslock);
  1766. if ((swrm->state == SWR_MSTR_DOWN) ||
  1767. (swrm->state == SWR_MSTR_SSR && swrm->dev_up)) {
  1768. if (swrm->clk_stop_mode0_supp) {
  1769. if (swrm->ipc_wakeup)
  1770. msm_aud_evt_blocking_notifier_call_chain(
  1771. SWR_WAKE_IRQ_DEREGISTER, (void *)swrm);
  1772. }
  1773. if (swrm_clk_request(swrm, true))
  1774. goto exit;
  1775. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  1776. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1777. ret = swr_device_up(swr_dev);
  1778. if (ret) {
  1779. dev_err(dev,
  1780. "%s: failed to wakeup swr dev %d\n",
  1781. __func__, swr_dev->dev_num);
  1782. swrm_clk_request(swrm, false);
  1783. goto exit;
  1784. }
  1785. }
  1786. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1787. swr_master_write(swrm, SWRM_COMP_SW_RESET, 0x01);
  1788. swrm_master_init(swrm);
  1789. swrm_cmd_fifo_wr_cmd(swrm, 0x4, 0xF, 0x0,
  1790. SWRS_SCP_INT_STATUS_MASK_1);
  1791. } else {
  1792. /*wake up from clock stop*/
  1793. swr_master_write(swrm, SWRM_MCP_BUS_CTRL_ADDR, 0x2);
  1794. usleep_range(100, 105);
  1795. }
  1796. swrm->state = SWR_MSTR_UP;
  1797. }
  1798. exit:
  1799. pm_runtime_set_autosuspend_delay(&pdev->dev, auto_suspend_timer);
  1800. mutex_unlock(&swrm->reslock);
  1801. return ret;
  1802. }
  1803. static int swrm_runtime_suspend(struct device *dev)
  1804. {
  1805. struct platform_device *pdev = to_platform_device(dev);
  1806. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1807. int ret = 0;
  1808. struct swr_master *mstr = &swrm->master;
  1809. struct swr_device *swr_dev;
  1810. int current_state = 0;
  1811. dev_dbg(dev, "%s: pm_runtime: suspend state: %d\n",
  1812. __func__, swrm->state);
  1813. mutex_lock(&swrm->reslock);
  1814. mutex_lock(&swrm->force_down_lock);
  1815. current_state = swrm->state;
  1816. mutex_unlock(&swrm->force_down_lock);
  1817. if ((current_state == SWR_MSTR_UP) ||
  1818. (current_state == SWR_MSTR_SSR)) {
  1819. if ((current_state != SWR_MSTR_SSR) &&
  1820. swrm_is_port_en(&swrm->master)) {
  1821. dev_dbg(dev, "%s ports are enabled\n", __func__);
  1822. ret = -EBUSY;
  1823. goto exit;
  1824. }
  1825. if (!swrm->clk_stop_mode0_supp || swrm->state == SWR_MSTR_SSR) {
  1826. swrm_clk_pause(swrm);
  1827. swr_master_write(swrm, SWRM_COMP_CFG_ADDR, 0x00);
  1828. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1829. ret = swr_device_down(swr_dev);
  1830. if (ret) {
  1831. dev_err(dev,
  1832. "%s: failed to shutdown swr dev %d\n",
  1833. __func__, swr_dev->dev_num);
  1834. goto exit;
  1835. }
  1836. }
  1837. } else {
  1838. /* clock stop sequence */
  1839. swrm_cmd_fifo_wr_cmd(swrm, 0x2, 0xF, 0xF,
  1840. SWRS_SCP_CONTROL);
  1841. usleep_range(100, 105);
  1842. }
  1843. swrm_clk_request(swrm, false);
  1844. if (swrm->clk_stop_mode0_supp) {
  1845. if (swrm->wake_irq > 0) {
  1846. enable_irq(swrm->wake_irq);
  1847. } else if (swrm->ipc_wakeup) {
  1848. msm_aud_evt_blocking_notifier_call_chain(
  1849. SWR_WAKE_IRQ_REGISTER, (void *)swrm);
  1850. swrm->ipc_wakeup_triggered = false;
  1851. }
  1852. }
  1853. }
  1854. /* Retain SSR state until resume */
  1855. if (current_state != SWR_MSTR_SSR)
  1856. swrm->state = SWR_MSTR_DOWN;
  1857. exit:
  1858. mutex_unlock(&swrm->reslock);
  1859. return ret;
  1860. }
  1861. #endif /* CONFIG_PM */
  1862. static int swrm_device_down(struct device *dev)
  1863. {
  1864. struct platform_device *pdev = to_platform_device(dev);
  1865. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  1866. int ret = 0;
  1867. dev_dbg(dev, "%s: swrm state: %d\n", __func__, swrm->state);
  1868. mutex_lock(&swrm->force_down_lock);
  1869. swrm->state = SWR_MSTR_SSR;
  1870. mutex_unlock(&swrm->force_down_lock);
  1871. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  1872. ret = swrm_runtime_suspend(dev);
  1873. if (!ret) {
  1874. pm_runtime_disable(dev);
  1875. pm_runtime_set_suspended(dev);
  1876. pm_runtime_enable(dev);
  1877. }
  1878. }
  1879. return 0;
  1880. }
  1881. int swrm_register_wake_irq(struct swr_mstr_ctrl *swrm)
  1882. {
  1883. int ret = 0;
  1884. if (!swrm->ipc_wakeup) {
  1885. swrm->wake_irq = platform_get_irq_byname(swrm->pdev,
  1886. "swr_wake_irq");
  1887. if (swrm->wake_irq < 0) {
  1888. dev_err(swrm->dev,
  1889. "%s() error getting wake irq handle: %d\n",
  1890. __func__, swrm->wake_irq);
  1891. return -EINVAL;
  1892. }
  1893. ret = request_threaded_irq(swrm->wake_irq, NULL,
  1894. swrm_wakeup_interrupt,
  1895. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1896. "swr_wake_irq", swrm);
  1897. if (ret) {
  1898. dev_err(swrm->dev, "%s: Failed to request irq %d\n",
  1899. __func__, ret);
  1900. return -EINVAL;
  1901. }
  1902. /* Disable wake irq - enable it after clock stop */
  1903. disable_irq(swrm->wake_irq);
  1904. }
  1905. return ret;
  1906. }
  1907. /**
  1908. * swrm_wcd_notify - parent device can notify to soundwire master through
  1909. * this function
  1910. * @pdev: pointer to platform device structure
  1911. * @id: command id from parent to the soundwire master
  1912. * @data: data from parent device to soundwire master
  1913. */
  1914. int swrm_wcd_notify(struct platform_device *pdev, u32 id, void *data)
  1915. {
  1916. struct swr_mstr_ctrl *swrm;
  1917. int ret = 0;
  1918. struct swr_master *mstr;
  1919. struct swr_device *swr_dev;
  1920. if (!pdev) {
  1921. pr_err("%s: pdev is NULL\n", __func__);
  1922. return -EINVAL;
  1923. }
  1924. swrm = platform_get_drvdata(pdev);
  1925. if (!swrm) {
  1926. dev_err(&pdev->dev, "%s: swrm is NULL\n", __func__);
  1927. return -EINVAL;
  1928. }
  1929. mstr = &swrm->master;
  1930. switch (id) {
  1931. case SWR_CLK_FREQ:
  1932. if (!data) {
  1933. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  1934. ret = -EINVAL;
  1935. } else {
  1936. mutex_lock(&swrm->mlock);
  1937. swrm->mclk_freq = *(int *)data;
  1938. mutex_unlock(&swrm->mlock);
  1939. }
  1940. break;
  1941. case SWR_DEVICE_SSR_DOWN:
  1942. mutex_lock(&swrm->devlock);
  1943. swrm->dev_up = false;
  1944. mutex_unlock(&swrm->devlock);
  1945. mutex_lock(&swrm->reslock);
  1946. swrm->state = SWR_MSTR_SSR;
  1947. mutex_unlock(&swrm->reslock);
  1948. break;
  1949. case SWR_DEVICE_SSR_UP:
  1950. /* wait for clk voting to be zero */
  1951. reinit_completion(&swrm->clk_off_complete);
  1952. if (swrm->clk_ref_count &&
  1953. !wait_for_completion_timeout(&swrm->clk_off_complete,
  1954. msecs_to_jiffies(200)))
  1955. dev_err(swrm->dev, "%s: clock voting not zero\n",
  1956. __func__);
  1957. mutex_lock(&swrm->devlock);
  1958. swrm->dev_up = true;
  1959. mutex_unlock(&swrm->devlock);
  1960. break;
  1961. case SWR_DEVICE_DOWN:
  1962. dev_dbg(swrm->dev, "%s: swr master down called\n", __func__);
  1963. mutex_lock(&swrm->mlock);
  1964. if (swrm->state == SWR_MSTR_DOWN)
  1965. dev_dbg(swrm->dev, "%s:SWR master is already Down:%d\n",
  1966. __func__, swrm->state);
  1967. else
  1968. swrm_device_down(&pdev->dev);
  1969. mutex_unlock(&swrm->mlock);
  1970. break;
  1971. case SWR_DEVICE_UP:
  1972. dev_dbg(swrm->dev, "%s: swr master up called\n", __func__);
  1973. mutex_lock(&swrm->devlock);
  1974. if (!swrm->dev_up) {
  1975. dev_dbg(swrm->dev, "SSR not complete yet\n");
  1976. mutex_unlock(&swrm->devlock);
  1977. return -EBUSY;
  1978. }
  1979. mutex_unlock(&swrm->devlock);
  1980. mutex_lock(&swrm->mlock);
  1981. pm_runtime_mark_last_busy(&pdev->dev);
  1982. pm_runtime_get_sync(&pdev->dev);
  1983. mutex_lock(&swrm->reslock);
  1984. list_for_each_entry(swr_dev, &mstr->devices, dev_list) {
  1985. ret = swr_reset_device(swr_dev);
  1986. if (ret) {
  1987. dev_err(swrm->dev,
  1988. "%s: failed to reset swr device %d\n",
  1989. __func__, swr_dev->dev_num);
  1990. swrm_clk_request(swrm, false);
  1991. }
  1992. }
  1993. pm_runtime_mark_last_busy(&pdev->dev);
  1994. pm_runtime_put_autosuspend(&pdev->dev);
  1995. mutex_unlock(&swrm->reslock);
  1996. mutex_unlock(&swrm->mlock);
  1997. break;
  1998. case SWR_SET_NUM_RX_CH:
  1999. if (!data) {
  2000. dev_err(swrm->dev, "%s: data is NULL\n", __func__);
  2001. ret = -EINVAL;
  2002. } else {
  2003. mutex_lock(&swrm->mlock);
  2004. swrm->num_rx_chs = *(int *)data;
  2005. if ((swrm->num_rx_chs > 1) && !swrm->num_cfg_devs) {
  2006. list_for_each_entry(swr_dev, &mstr->devices,
  2007. dev_list) {
  2008. ret = swr_set_device_group(swr_dev,
  2009. SWR_BROADCAST);
  2010. if (ret)
  2011. dev_err(swrm->dev,
  2012. "%s: set num ch failed\n",
  2013. __func__);
  2014. }
  2015. } else {
  2016. list_for_each_entry(swr_dev, &mstr->devices,
  2017. dev_list) {
  2018. ret = swr_set_device_group(swr_dev,
  2019. SWR_GROUP_NONE);
  2020. if (ret)
  2021. dev_err(swrm->dev,
  2022. "%s: set num ch failed\n",
  2023. __func__);
  2024. }
  2025. }
  2026. mutex_unlock(&swrm->mlock);
  2027. }
  2028. break;
  2029. case SWR_REGISTER_WAKE_IRQ:
  2030. if (!data) {
  2031. dev_err(swrm->dev, "%s: reg wake irq data is NULL\n",
  2032. __func__);
  2033. ret = -EINVAL;
  2034. } else {
  2035. mutex_lock(&swrm->mlock);
  2036. swrm->ipc_wakeup = *(u32 *)data;
  2037. ret = swrm_register_wake_irq(swrm);
  2038. if (ret)
  2039. dev_err(swrm->dev, "%s: register wake_irq failed\n",
  2040. __func__);
  2041. mutex_unlock(&swrm->mlock);
  2042. }
  2043. break;
  2044. default:
  2045. dev_err(swrm->dev, "%s: swr master unknown id %d\n",
  2046. __func__, id);
  2047. break;
  2048. }
  2049. return ret;
  2050. }
  2051. EXPORT_SYMBOL(swrm_wcd_notify);
  2052. #ifdef CONFIG_PM_SLEEP
  2053. static int swrm_suspend(struct device *dev)
  2054. {
  2055. int ret = -EBUSY;
  2056. struct platform_device *pdev = to_platform_device(dev);
  2057. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2058. dev_dbg(dev, "%s: system suspend, state: %d\n", __func__, swrm->state);
  2059. if (!pm_runtime_enabled(dev) || !pm_runtime_suspended(dev)) {
  2060. ret = swrm_runtime_suspend(dev);
  2061. if (!ret) {
  2062. /*
  2063. * Synchronize runtime-pm and system-pm states:
  2064. * At this point, we are already suspended. If
  2065. * runtime-pm still thinks its active, then
  2066. * make sure its status is in sync with HW
  2067. * status. The three below calls let the
  2068. * runtime-pm know that we are suspended
  2069. * already without re-invoking the suspend
  2070. * callback
  2071. */
  2072. pm_runtime_disable(dev);
  2073. pm_runtime_set_suspended(dev);
  2074. pm_runtime_enable(dev);
  2075. }
  2076. }
  2077. if (ret == -EBUSY) {
  2078. /*
  2079. * There is a possibility that some audio stream is active
  2080. * during suspend. We dont want to return suspend failure in
  2081. * that case so that display and relevant components can still
  2082. * go to suspend.
  2083. * If there is some other error, then it should be passed-on
  2084. * to system level suspend
  2085. */
  2086. ret = 0;
  2087. }
  2088. return ret;
  2089. }
  2090. static int swrm_resume(struct device *dev)
  2091. {
  2092. int ret = 0;
  2093. struct platform_device *pdev = to_platform_device(dev);
  2094. struct swr_mstr_ctrl *swrm = platform_get_drvdata(pdev);
  2095. dev_dbg(dev, "%s: system resume, state: %d\n", __func__, swrm->state);
  2096. if (!pm_runtime_enabled(dev) || !pm_runtime_suspend(dev)) {
  2097. ret = swrm_runtime_resume(dev);
  2098. if (!ret) {
  2099. pm_runtime_mark_last_busy(dev);
  2100. pm_request_autosuspend(dev);
  2101. }
  2102. }
  2103. return ret;
  2104. }
  2105. #endif /* CONFIG_PM_SLEEP */
  2106. static const struct dev_pm_ops swrm_dev_pm_ops = {
  2107. SET_SYSTEM_SLEEP_PM_OPS(
  2108. swrm_suspend,
  2109. swrm_resume
  2110. )
  2111. SET_RUNTIME_PM_OPS(
  2112. swrm_runtime_suspend,
  2113. swrm_runtime_resume,
  2114. NULL
  2115. )
  2116. };
  2117. static const struct of_device_id swrm_dt_match[] = {
  2118. {
  2119. .compatible = "qcom,swr-mstr",
  2120. },
  2121. {}
  2122. };
  2123. static struct platform_driver swr_mstr_driver = {
  2124. .probe = swrm_probe,
  2125. .remove = swrm_remove,
  2126. .driver = {
  2127. .name = SWR_WCD_NAME,
  2128. .owner = THIS_MODULE,
  2129. .pm = &swrm_dev_pm_ops,
  2130. .of_match_table = swrm_dt_match,
  2131. },
  2132. };
  2133. static int __init swrm_init(void)
  2134. {
  2135. return platform_driver_register(&swr_mstr_driver);
  2136. }
  2137. module_init(swrm_init);
  2138. static void __exit swrm_exit(void)
  2139. {
  2140. platform_driver_unregister(&swr_mstr_driver);
  2141. }
  2142. module_exit(swrm_exit);
  2143. MODULE_LICENSE("GPL v2");
  2144. MODULE_DESCRIPTION("SoundWire Master Controller");
  2145. MODULE_ALIAS("platform:swr-mstr");