cs40l26-sysfs.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // cs40l26-sysfs.c -- CS40L26 Boosted Haptic Driver with Integrated DSP and
  4. // Waveform Memory with Advanced Closed Loop Algorithms and LRA protection
  5. //
  6. // Copyright 2022 Cirrus Logic, Inc.
  7. //
  8. // Author: Fred Treven <[email protected]>
  9. //
  10. // This program is free software; you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License version 2 as
  12. // published by the Free Software Foundation.
  13. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  14. #include <linux/vibrator/cs40l26.h>
  15. #else
  16. #include <linux/mfd/cs40l26.h>
  17. #endif
  18. static ssize_t dsp_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  19. {
  20. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  21. u8 dsp_state;
  22. int error;
  23. error = cs40l26_pm_enter(cs40l26->dev);
  24. if (error)
  25. return error;
  26. error = cs40l26_dsp_state_get(cs40l26, &dsp_state);
  27. cs40l26_pm_exit(cs40l26->dev);
  28. if (error)
  29. return error;
  30. else
  31. return snprintf(buf, PAGE_SIZE, "%u\n", (unsigned int) (dsp_state & 0xFF));
  32. }
  33. static DEVICE_ATTR_RO(dsp_state);
  34. static ssize_t owt_lib_compat_show(struct device *dev, struct device_attribute *attr, char *buf)
  35. {
  36. return snprintf(buf, PAGE_SIZE, "1.0.0\n");
  37. }
  38. static DEVICE_ATTR_RO(owt_lib_compat);
  39. static ssize_t overprotection_gain_show(struct device *dev, struct device_attribute *attr,
  40. char *buf)
  41. {
  42. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  43. u32 reg, op_gain;
  44. int error;
  45. if (!cl_dsp_algo_is_present(cs40l26->dsp, CS40L26_EP_ALGO_ID))
  46. return -EPERM;
  47. error = cl_dsp_get_reg(cs40l26->dsp, "PROTECTION_XM_OP_GAIN",
  48. CL_DSP_XM_UNPACKED_TYPE, CS40L26_EP_ALGO_ID, &reg);
  49. if (error)
  50. return error;
  51. error = cs40l26_pm_enter(cs40l26->dev);
  52. if (error)
  53. return error;
  54. error = regmap_read(cs40l26->regmap, reg, &op_gain);
  55. cs40l26_pm_exit(cs40l26->dev);
  56. return error ? error : snprintf(buf, PAGE_SIZE, "%d\n", op_gain);
  57. }
  58. static ssize_t overprotection_gain_store(struct device *dev, struct device_attribute *attr,
  59. const char *buf, size_t count)
  60. {
  61. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  62. u32 reg, op_gain;
  63. int error;
  64. if (!cl_dsp_algo_is_present(cs40l26->dsp, CS40L26_EP_ALGO_ID))
  65. return -EPERM;
  66. error = kstrtou32(buf, 10, &op_gain);
  67. if (error || op_gain < CS40L26_OVERPROTECTION_GAIN_MIN ||
  68. op_gain > CS40L26_UINT_24_BITS_MAX)
  69. return -EINVAL;
  70. error = cs40l26_pm_enter(cs40l26->dev);
  71. if (error)
  72. return error;
  73. error = cl_dsp_get_reg(cs40l26->dsp, "PROTECTION_XM_OP_GAIN",
  74. CL_DSP_XM_UNPACKED_TYPE, CS40L26_EP_ALGO_ID, &reg);
  75. if (error)
  76. goto err_pm;
  77. error = regmap_write(cs40l26->regmap, reg, op_gain);
  78. err_pm:
  79. cs40l26_pm_exit(cs40l26->dev);
  80. return error ? error : count;
  81. }
  82. static DEVICE_ATTR_RW(overprotection_gain);
  83. static ssize_t halo_heartbeat_show(struct device *dev, struct device_attribute *attr, char *buf)
  84. {
  85. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  86. u32 reg, halo_heartbeat;
  87. int error;
  88. error = cl_dsp_get_reg(cs40l26->dsp, "HALO_HEARTBEAT", CL_DSP_XM_UNPACKED_TYPE,
  89. cs40l26->fw_id, &reg);
  90. if (error)
  91. return error;
  92. error = cs40l26_pm_enter(cs40l26->dev);
  93. if (error)
  94. return error;
  95. error = regmap_read(cs40l26->regmap, reg, &halo_heartbeat);
  96. cs40l26_pm_exit(cs40l26->dev);
  97. if (error)
  98. return error;
  99. return snprintf(buf, PAGE_SIZE, "%d\n", halo_heartbeat);
  100. }
  101. static DEVICE_ATTR_RO(halo_heartbeat);
  102. static ssize_t pm_stdby_timeout_ms_show(struct device *dev, struct device_attribute *attr,
  103. char *buf)
  104. {
  105. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  106. u32 timeout_ms;
  107. int error;
  108. error = cs40l26_pm_enter(cs40l26->dev);
  109. if (error)
  110. return error;
  111. error = cs40l26_pm_timeout_ms_get(cs40l26, CS40L26_DSP_STATE_STANDBY, &timeout_ms);
  112. cs40l26_pm_exit(cs40l26->dev);
  113. if (error)
  114. return error;
  115. return snprintf(buf, PAGE_SIZE, "%u\n", timeout_ms);
  116. }
  117. static ssize_t pm_stdby_timeout_ms_store(struct device *dev,
  118. struct device_attribute *attr, const char *buf, size_t count)
  119. {
  120. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  121. u32 timeout_ms;
  122. int error;
  123. error = kstrtou32(buf, 10, &timeout_ms);
  124. if (error)
  125. return -EINVAL;
  126. error = cs40l26_pm_enter(cs40l26->dev);
  127. if (error)
  128. return error;
  129. error = cs40l26_pm_timeout_ms_set(cs40l26, CS40L26_DSP_STATE_STANDBY, timeout_ms);
  130. cs40l26_pm_exit(cs40l26->dev);
  131. if (error)
  132. return error;
  133. return count;
  134. }
  135. static DEVICE_ATTR_RW(pm_stdby_timeout_ms);
  136. static ssize_t pm_active_timeout_ms_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  140. u32 timeout_ms;
  141. int error;
  142. error = cs40l26_pm_enter(cs40l26->dev);
  143. if (error)
  144. return error;
  145. error = cs40l26_pm_timeout_ms_get(cs40l26, CS40L26_DSP_STATE_ACTIVE, &timeout_ms);
  146. cs40l26_pm_exit(cs40l26->dev);
  147. if (error)
  148. return error;
  149. return snprintf(buf, PAGE_SIZE, "%u\n", timeout_ms);
  150. }
  151. static ssize_t pm_active_timeout_ms_store(struct device *dev,
  152. struct device_attribute *attr, const char *buf, size_t count)
  153. {
  154. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  155. u32 timeout_ms;
  156. int error;
  157. error = kstrtou32(buf, 10, &timeout_ms);
  158. if (error)
  159. return -EINVAL;
  160. error = cs40l26_pm_enter(cs40l26->dev);
  161. if (error)
  162. return error;
  163. error = cs40l26_pm_timeout_ms_set(cs40l26, CS40L26_DSP_STATE_ACTIVE, timeout_ms);
  164. cs40l26_pm_exit(cs40l26->dev);
  165. if (error)
  166. return error;
  167. return count;
  168. }
  169. static DEVICE_ATTR_RW(pm_active_timeout_ms);
  170. static ssize_t vibe_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  171. {
  172. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  173. unsigned int state;
  174. if (!cs40l26->vibe_state_reporting) {
  175. dev_err(cs40l26->dev, "vibe_state not supported\n");
  176. return -EPERM;
  177. }
  178. mutex_lock(&cs40l26->lock);
  179. state = cs40l26->vibe_state;
  180. mutex_unlock(&cs40l26->lock);
  181. return snprintf(buf, PAGE_SIZE, "%u\n", state);
  182. }
  183. static DEVICE_ATTR_RO(vibe_state);
  184. static ssize_t power_on_seq_show(struct device *dev, struct device_attribute *attr, char *buf)
  185. {
  186. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  187. struct cs40l26_pseq_op *op;
  188. u32 addr, data, base;
  189. int error;
  190. mutex_lock(&cs40l26->lock);
  191. base = cs40l26->pseq_base;
  192. if (list_empty(&cs40l26->pseq_op_head)) {
  193. dev_err(cs40l26->dev, "Power on sequence is empty\n");
  194. error = -EINVAL;
  195. goto err_mutex;
  196. }
  197. list_for_each_entry_reverse(op, &cs40l26->pseq_op_head, list) {
  198. switch (op->operation) {
  199. case CS40L26_PSEQ_OP_WRITE_FULL:
  200. addr = ((op->words[0] & 0xFFFF) << 16) | ((op->words[1] & 0x00FFFF00) >> 8);
  201. data = ((op->words[1] & 0xFF) << 24) | (op->words[2] & 0xFFFFFF);
  202. break;
  203. case CS40L26_PSEQ_OP_WRITE_H16:
  204. case CS40L26_PSEQ_OP_WRITE_L16:
  205. addr = ((op->words[0] & 0xFFFF) << 8) | ((op->words[1] & 0xFF0000) >> 16);
  206. data = (op->words[1] & 0xFFFF);
  207. if (op->operation == CS40L26_PSEQ_OP_WRITE_H16)
  208. data <<= 16;
  209. break;
  210. case CS40L26_PSEQ_OP_WRITE_ADDR8:
  211. addr = (op->words[0] & 0xFF00) >> 8;
  212. data = ((op->words[0] & 0xFF) << 24) | (op->words[1] & 0xFFFFFF);
  213. break;
  214. case CS40L26_PSEQ_OP_END:
  215. addr = CS40L26_PSEQ_OP_END_ADDR;
  216. data = CS40L26_PSEQ_OP_END_DATA;
  217. break;
  218. default:
  219. dev_err(cs40l26->dev, "Unrecognized Op Code: 0x%02X\n", op->operation);
  220. error = -EINVAL;
  221. goto err_mutex;
  222. }
  223. dev_dbg(cs40l26->dev, "0x%08x: code = 0x%02X, Addr = 0x%08X, Data = 0x%08X\n",
  224. base + op->offset, op->operation, addr, data);
  225. }
  226. error = snprintf(buf, PAGE_SIZE, "%d\n", cs40l26->pseq_num_ops);
  227. err_mutex:
  228. mutex_unlock(&cs40l26->lock);
  229. return error;
  230. }
  231. static DEVICE_ATTR_RO(power_on_seq);
  232. static ssize_t owt_free_space_show(struct device *dev,
  233. struct device_attribute *attr, char *buf)
  234. {
  235. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  236. u32 reg, words;
  237. int error;
  238. error = cs40l26_pm_enter(cs40l26->dev);
  239. if (error)
  240. return error;
  241. error = cl_dsp_get_reg(cs40l26->dsp, "OWT_SIZE_XM",
  242. CL_DSP_XM_UNPACKED_TYPE, CS40L26_VIBEGEN_ALGO_ID, &reg);
  243. if (error)
  244. goto err_pm;
  245. error = regmap_read(cs40l26->regmap, reg, &words);
  246. if (error) {
  247. dev_err(cs40l26->dev, "Failed to get remaining OWT space\n");
  248. goto err_pm;
  249. }
  250. error = snprintf(buf, PAGE_SIZE, "%d\n", words * CL_DSP_BYTES_PER_WORD);
  251. err_pm:
  252. cs40l26_pm_exit(cs40l26->dev);
  253. return error;
  254. }
  255. static DEVICE_ATTR_RO(owt_free_space);
  256. static ssize_t die_temp_show(struct device *dev,
  257. struct device_attribute *attr, char *buf)
  258. {
  259. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  260. struct regmap *regmap = cs40l26->regmap;
  261. u16 die_temp;
  262. int error;
  263. u32 val;
  264. error = cs40l26_pm_enter(cs40l26->dev);
  265. if (error)
  266. return error;
  267. error = regmap_read(regmap, CS40L26_GLOBAL_ENABLES, &val);
  268. if (error) {
  269. dev_err(cs40l26->dev, "Failed to read GLOBAL_EN status\n");
  270. goto err_pm;
  271. }
  272. if (!(val & CS40L26_GLOBAL_EN_MASK)) {
  273. dev_err(cs40l26->dev, "Global enable must be set to get die temp.\n");
  274. error = -EPERM;
  275. goto err_pm;
  276. }
  277. error = regmap_read(regmap, CS40L26_ENABLES_AND_CODES_DIG, &val);
  278. if (error) {
  279. dev_err(cs40l26->dev, "Failed to get die temperature\n");
  280. goto err_pm;
  281. }
  282. die_temp = (val & CS40L26_TEMP_RESULT_FILT_MASK) >> CS40L26_TEMP_RESULT_FILT_SHIFT;
  283. error = snprintf(buf, PAGE_SIZE, "0x%03X\n", die_temp);
  284. err_pm:
  285. cs40l26_pm_exit(cs40l26->dev);
  286. return error;
  287. }
  288. static DEVICE_ATTR_RO(die_temp);
  289. static ssize_t num_waves_show(struct device *dev, struct device_attribute *attr, char *buf)
  290. {
  291. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  292. u32 nwaves;
  293. int error;
  294. error = cs40l26_pm_enter(cs40l26->dev);
  295. if (error)
  296. return error;
  297. error = cs40l26_get_num_waves(cs40l26, &nwaves);
  298. if (error)
  299. goto err_pm;
  300. error = snprintf(buf, PAGE_SIZE, "%u\n", nwaves);
  301. err_pm:
  302. cs40l26_pm_exit(cs40l26->dev);
  303. return error;
  304. }
  305. static DEVICE_ATTR_RO(num_waves);
  306. /* boost_disable_delay is in units of 125us, e.g. 8 -> 1ms */
  307. static ssize_t boost_disable_delay_show(struct device *dev, struct device_attribute *attr,
  308. char *buf)
  309. {
  310. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  311. u32 reg, boost_disable_delay;
  312. int error;
  313. error = cs40l26_pm_enter(cs40l26->dev);
  314. if (error)
  315. return error;
  316. error = cl_dsp_get_reg(cs40l26->dsp, "BOOST_DISABLE_DELAY",
  317. CL_DSP_XM_UNPACKED_TYPE, CS40L26_EXT_ALGO_ID, &reg);
  318. if (error)
  319. goto err_pm;
  320. error = regmap_read(cs40l26->regmap, reg, &boost_disable_delay);
  321. if (error)
  322. goto err_pm;
  323. error = snprintf(buf, PAGE_SIZE, "%d\n", boost_disable_delay);
  324. err_pm:
  325. cs40l26_pm_exit(cs40l26->dev);
  326. return error;
  327. }
  328. static ssize_t boost_disable_delay_store(struct device *dev,
  329. struct device_attribute *attr, const char *buf, size_t count)
  330. {
  331. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  332. u32 reg, boost_disable_delay;
  333. int error;
  334. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  335. dev_info(cs40l26->dev, "%s: %s", __func__, buf);
  336. #else
  337. dev_dbg(cs40l26->dev, "%s: %s", __func__, buf);
  338. #endif
  339. error = kstrtou32(buf, 10, &boost_disable_delay);
  340. if (error || boost_disable_delay < CS40L26_BOOST_DISABLE_DELAY_MIN ||
  341. boost_disable_delay > CS40L26_BOOST_DISABLE_DELAY_MAX)
  342. return -EINVAL;
  343. error = cs40l26_pm_enter(cs40l26->dev);
  344. if (error)
  345. return error;
  346. error = cl_dsp_get_reg(cs40l26->dsp, "BOOST_DISABLE_DELAY",
  347. CL_DSP_XM_UNPACKED_TYPE, CS40L26_EXT_ALGO_ID, &reg);
  348. if (error)
  349. goto err_pm;
  350. error = regmap_write(cs40l26->regmap, reg, boost_disable_delay);
  351. err_pm:
  352. cs40l26_pm_exit(cs40l26->dev);
  353. if (error)
  354. return error;
  355. else
  356. return count;
  357. }
  358. static DEVICE_ATTR_RW(boost_disable_delay);
  359. static ssize_t f0_offset_show(struct device *dev, struct device_attribute *attr, char *buf)
  360. {
  361. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  362. unsigned int reg, val;
  363. int error;
  364. error = cs40l26_pm_enter(cs40l26->dev);
  365. if (error)
  366. return error;
  367. mutex_lock(&cs40l26->lock);
  368. error = cl_dsp_get_reg(cs40l26->dsp, "F0_OFFSET", CL_DSP_XM_UNPACKED_TYPE,
  369. CS40L26_VIBEGEN_ALGO_ID, &reg);
  370. if (error)
  371. goto err_mutex;
  372. error = regmap_read(cs40l26->regmap, reg, &val);
  373. if (error)
  374. goto err_mutex;
  375. error = snprintf(buf, PAGE_SIZE, "%u\n", val);
  376. err_mutex:
  377. mutex_unlock(&cs40l26->lock);
  378. cs40l26_pm_exit(cs40l26->dev);
  379. return error;
  380. }
  381. static ssize_t f0_offset_store(struct device *dev, struct device_attribute *attr, const char *buf,
  382. size_t count)
  383. {
  384. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  385. unsigned int reg, val;
  386. int error;
  387. error = kstrtou32(buf, 10, &val);
  388. if (error)
  389. return -EINVAL;
  390. if (val > CS40L26_F0_OFFSET_MAX && val < CS40L26_F0_OFFSET_MIN)
  391. return -EINVAL;
  392. error = cs40l26_pm_enter(cs40l26->dev);
  393. if (error)
  394. return error;
  395. mutex_lock(&cs40l26->lock);
  396. error = cl_dsp_get_reg(cs40l26->dsp, "F0_OFFSET", CL_DSP_XM_UNPACKED_TYPE,
  397. CS40L26_VIBEGEN_ALGO_ID, &reg);
  398. if (error)
  399. goto err_mutex;
  400. error = regmap_write(cs40l26->regmap, reg, val);
  401. if (error)
  402. goto err_mutex;
  403. error = count;
  404. err_mutex:
  405. mutex_unlock(&cs40l26->lock);
  406. cs40l26_pm_exit(cs40l26->dev);
  407. return error;
  408. }
  409. static DEVICE_ATTR_RW(f0_offset);
  410. static ssize_t delay_before_stop_playback_us_show(struct device *dev,
  411. struct device_attribute *attr, char *buf)
  412. {
  413. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  414. int error;
  415. mutex_lock(&cs40l26->lock);
  416. error = snprintf(buf, PAGE_SIZE, "%d\n", cs40l26->delay_before_stop_playback_us);
  417. mutex_unlock(&cs40l26->lock);
  418. return error;
  419. }
  420. static ssize_t delay_before_stop_playback_us_store(struct device *dev,
  421. struct device_attribute *attr, const char *buf, size_t count)
  422. {
  423. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  424. u32 val;
  425. int error;
  426. error = kstrtou32(buf, 10, &val);
  427. if (error)
  428. return -EINVAL;
  429. mutex_lock(&cs40l26->lock);
  430. cs40l26->delay_before_stop_playback_us = val;
  431. mutex_unlock(&cs40l26->lock);
  432. return count;
  433. }
  434. static DEVICE_ATTR_RW(delay_before_stop_playback_us);
  435. static ssize_t f0_comp_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  436. {
  437. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  438. int error;
  439. mutex_lock(&cs40l26->lock);
  440. if (cs40l26->fw_id == CS40L26_FW_CALIB_ID) {
  441. error = -EPERM;
  442. goto err_mutex;
  443. }
  444. if (cs40l26->comp_enable_pend) {
  445. error = -EIO;
  446. goto err_mutex;
  447. }
  448. error = snprintf(buf, PAGE_SIZE, "%d\n", cs40l26->comp_enable_f0);
  449. err_mutex:
  450. mutex_unlock(&cs40l26->lock);
  451. return error;
  452. }
  453. static ssize_t f0_comp_enable_store(struct device *dev, struct device_attribute *attr,
  454. const char *buf, size_t count)
  455. {
  456. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  457. int error;
  458. unsigned int val;
  459. u32 reg, value;
  460. error = kstrtou32(buf, 10, &val);
  461. if (error)
  462. return -EINVAL;
  463. error = cs40l26_pm_enter(cs40l26->dev);
  464. if (error)
  465. return error;
  466. mutex_lock(&cs40l26->lock);
  467. cs40l26->comp_enable_pend = true;
  468. cs40l26->comp_enable_f0 = val > 0;
  469. value = (cs40l26->comp_enable_redc << CS40L26_COMP_EN_REDC_SHIFT) |
  470. (cs40l26->comp_enable_f0 << CS40L26_COMP_EN_F0_SHIFT);
  471. if (cs40l26->fw_id == CS40L26_FW_CALIB_ID) {
  472. error = -EPERM;
  473. } else {
  474. error = cl_dsp_get_reg(cs40l26->dsp, "COMPENSATION_ENABLE", CL_DSP_XM_UNPACKED_TYPE,
  475. CS40L26_VIBEGEN_ALGO_ID, &reg);
  476. if (error)
  477. goto err_mutex;
  478. error = regmap_write(cs40l26->regmap, reg, value);
  479. }
  480. if (error)
  481. goto err_mutex;
  482. error = count;
  483. err_mutex:
  484. cs40l26->comp_enable_pend = false;
  485. mutex_unlock(&cs40l26->lock);
  486. cs40l26_pm_exit(cs40l26->dev);
  487. return error;
  488. }
  489. static DEVICE_ATTR_RW(f0_comp_enable);
  490. static ssize_t redc_comp_enable_show(struct device *dev, struct device_attribute *attr, char *buf)
  491. {
  492. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  493. int error;
  494. mutex_lock(&cs40l26->lock);
  495. if (cs40l26->fw_id == CS40L26_FW_CALIB_ID) {
  496. error = -EPERM;
  497. goto err_mutex;
  498. }
  499. if (cs40l26->comp_enable_pend) {
  500. error = -EIO;
  501. goto err_mutex;
  502. }
  503. error = snprintf(buf, PAGE_SIZE, "%d\n", cs40l26->comp_enable_redc);
  504. err_mutex:
  505. mutex_unlock(&cs40l26->lock);
  506. return error;
  507. }
  508. static ssize_t redc_comp_enable_store(struct device *dev, struct device_attribute *attr,
  509. const char *buf, size_t count)
  510. {
  511. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  512. int error;
  513. unsigned int val;
  514. u32 reg, value;
  515. error = kstrtou32(buf, 10, &val);
  516. if (error)
  517. return -EINVAL;
  518. error = cs40l26_pm_enter(cs40l26->dev);
  519. if (error)
  520. return error;
  521. mutex_lock(&cs40l26->lock);
  522. cs40l26->comp_enable_pend = true;
  523. cs40l26->comp_enable_redc = val > 0;
  524. value = (cs40l26->comp_enable_redc << CS40L26_COMP_EN_REDC_SHIFT) |
  525. (cs40l26->comp_enable_f0 << CS40L26_COMP_EN_F0_SHIFT);
  526. if (cs40l26->fw_id == CS40L26_FW_CALIB_ID) {
  527. error = -EPERM;
  528. } else {
  529. error = cl_dsp_get_reg(cs40l26->dsp, "COMPENSATION_ENABLE", CL_DSP_XM_UNPACKED_TYPE,
  530. CS40L26_VIBEGEN_ALGO_ID, &reg);
  531. if (error)
  532. goto err_mutex;
  533. error = regmap_write(cs40l26->regmap, reg, value);
  534. }
  535. if (error)
  536. goto err_mutex;
  537. error = count;
  538. err_mutex:
  539. cs40l26->comp_enable_pend = false;
  540. mutex_unlock(&cs40l26->lock);
  541. cs40l26_pm_exit(cs40l26->dev);
  542. return error;
  543. }
  544. static DEVICE_ATTR_RW(redc_comp_enable);
  545. static ssize_t swap_firmware_show(struct device *dev, struct device_attribute *attr, char *buf)
  546. {
  547. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  548. int error;
  549. mutex_lock(&cs40l26->lock);
  550. if (cs40l26->fw_id == CS40L26_FW_ID)
  551. error = snprintf(buf, PAGE_SIZE, "%d\n", 0);
  552. else if (cs40l26->fw_id == CS40L26_FW_CALIB_ID)
  553. error = snprintf(buf, PAGE_SIZE, "%d\n", 1);
  554. else
  555. error = -EINVAL;
  556. mutex_unlock(&cs40l26->lock);
  557. return error;
  558. }
  559. static ssize_t swap_firmware_store(struct device *dev, struct device_attribute *attr,
  560. const char *buf, size_t count)
  561. {
  562. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  563. int error;
  564. unsigned int variant;
  565. error = kstrtou32(buf, 10, &variant);
  566. if (error)
  567. return error;
  568. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  569. dev_info(cs40l26->dev, "%s %d\n", __func__, variant);
  570. #endif
  571. if (variant == 0)
  572. error = cs40l26_fw_swap(cs40l26, CS40L26_FW_ID);
  573. else if (variant == 1)
  574. error = cs40l26_fw_swap(cs40l26, CS40L26_FW_CALIB_ID);
  575. else
  576. error = -EINVAL;
  577. return error ? error : count;
  578. }
  579. static DEVICE_ATTR_RW(swap_firmware);
  580. static struct attribute *cs40l26_dev_attrs[] = {
  581. &dev_attr_num_waves.attr,
  582. &dev_attr_die_temp.attr,
  583. &dev_attr_owt_free_space.attr,
  584. &dev_attr_power_on_seq.attr,
  585. &dev_attr_dsp_state.attr,
  586. &dev_attr_halo_heartbeat.attr,
  587. &dev_attr_pm_stdby_timeout_ms.attr,
  588. &dev_attr_pm_active_timeout_ms.attr,
  589. &dev_attr_vibe_state.attr,
  590. &dev_attr_boost_disable_delay.attr,
  591. &dev_attr_f0_offset.attr,
  592. &dev_attr_delay_before_stop_playback_us.attr,
  593. &dev_attr_f0_comp_enable.attr,
  594. &dev_attr_redc_comp_enable.attr,
  595. &dev_attr_swap_firmware.attr,
  596. &dev_attr_owt_lib_compat.attr,
  597. &dev_attr_overprotection_gain.attr,
  598. NULL,
  599. };
  600. struct attribute_group cs40l26_dev_attr_group = {
  601. .name = "default",
  602. .attrs = cs40l26_dev_attrs,
  603. };
  604. static ssize_t dbc_enable_show(struct device *dev,
  605. struct device_attribute *attr, char *buf)
  606. {
  607. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  608. u32 val, reg;
  609. int error;
  610. error = cs40l26_pm_enter(cs40l26->dev);
  611. if (error)
  612. return error;
  613. mutex_lock(&cs40l26->lock);
  614. error = cl_dsp_get_reg(cs40l26->dsp, "FLAGS", CL_DSP_XM_UNPACKED_TYPE, CS40L26_EXT_ALGO_ID,
  615. &reg);
  616. if (error)
  617. goto err_pm;
  618. error = regmap_read(cs40l26->regmap, reg, &val);
  619. if (error) {
  620. dev_err(cs40l26->dev, "Failed to get FLAGS\n");
  621. goto err_pm;
  622. }
  623. val &= CS40L26_DBC_ENABLE_MASK;
  624. val >>= CS40L26_DBC_ENABLE_SHIFT;
  625. error = snprintf(buf, PAGE_SIZE, "%u\n", val);
  626. err_pm:
  627. mutex_unlock(&cs40l26->lock);
  628. cs40l26_pm_exit(cs40l26->dev);
  629. return error;
  630. }
  631. static ssize_t dbc_enable_store(struct device *dev,
  632. struct device_attribute *attr, const char *buf, size_t count)
  633. {
  634. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  635. int error;
  636. u32 val;
  637. error = kstrtou32(buf, 10, &val);
  638. if (error)
  639. return error;
  640. if (val > 1)
  641. return -EINVAL;
  642. error = cs40l26_pm_enter(cs40l26->dev);
  643. if (error)
  644. return error;
  645. mutex_lock(&cs40l26->lock);
  646. error = cs40l26_dbc_enable(cs40l26, val);
  647. mutex_unlock(&cs40l26->lock);
  648. cs40l26_pm_exit(cs40l26->dev);
  649. return error ? error : count;
  650. }
  651. static DEVICE_ATTR_RW(dbc_enable);
  652. static ssize_t dbc_env_rel_coef_show(struct device *dev, struct device_attribute *attr, char *buf)
  653. {
  654. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  655. unsigned int val;
  656. int error;
  657. error = cs40l26_dbc_get(cs40l26, CS40L26_DBC_ENV_REL_COEF, &val);
  658. return error ? error : snprintf(buf, PAGE_SIZE, "%u\n", val);
  659. }
  660. static ssize_t dbc_env_rel_coef_store(struct device *dev,
  661. struct device_attribute *attr, const char *buf, size_t count)
  662. {
  663. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  664. struct device *cdev = cs40l26->dev;
  665. u32 val;
  666. int error;
  667. error = kstrtou32(buf, 10, &val);
  668. if (error)
  669. return error;
  670. error = cs40l26_pm_enter(cdev);
  671. if (error)
  672. return error;
  673. mutex_lock(&cs40l26->lock);
  674. error = cs40l26_dbc_set(cs40l26, CS40L26_DBC_ENV_REL_COEF, val);
  675. mutex_unlock(&cs40l26->lock);
  676. cs40l26_pm_exit(cdev);
  677. return error ? error : count;
  678. }
  679. static DEVICE_ATTR_RW(dbc_env_rel_coef);
  680. static ssize_t dbc_rise_headroom_show(struct device *dev,
  681. struct device_attribute *attr, char *buf)
  682. {
  683. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  684. unsigned int val;
  685. int error;
  686. error = cs40l26_dbc_get(cs40l26, CS40L26_DBC_RISE_HEADROOM, &val);
  687. return error ? error : snprintf(buf, PAGE_SIZE, "%u\n", val);
  688. }
  689. static ssize_t dbc_rise_headroom_store(struct device *dev,
  690. struct device_attribute *attr, const char *buf, size_t count)
  691. {
  692. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  693. struct device *cdev = cs40l26->dev;
  694. u32 val;
  695. int error;
  696. error = kstrtou32(buf, 10, &val);
  697. if (error)
  698. return error;
  699. error = cs40l26_pm_enter(cdev);
  700. if (error)
  701. return error;
  702. mutex_lock(&cs40l26->lock);
  703. error = cs40l26_dbc_set(cs40l26, CS40L26_DBC_RISE_HEADROOM, val);
  704. mutex_unlock(&cs40l26->lock);
  705. cs40l26_pm_exit(cdev);
  706. return error ? error : count;
  707. }
  708. static DEVICE_ATTR_RW(dbc_rise_headroom);
  709. static ssize_t dbc_fall_headroom_show(struct device *dev, struct device_attribute *attr, char *buf)
  710. {
  711. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  712. unsigned int val;
  713. int error;
  714. error = cs40l26_dbc_get(cs40l26, CS40L26_DBC_FALL_HEADROOM, &val);
  715. return error ? error : snprintf(buf, PAGE_SIZE, "%u\n", val);
  716. }
  717. static ssize_t dbc_fall_headroom_store(struct device *dev,
  718. struct device_attribute *attr, const char *buf, size_t count)
  719. {
  720. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  721. struct device *cdev = cs40l26->dev;
  722. u32 val;
  723. int error;
  724. error = kstrtou32(buf, 10, &val);
  725. if (error)
  726. return error;
  727. error = cs40l26_pm_enter(cdev);
  728. if (error)
  729. return error;
  730. mutex_lock(&cs40l26->lock);
  731. error = cs40l26_dbc_set(cs40l26, CS40L26_DBC_FALL_HEADROOM, val);
  732. mutex_unlock(&cs40l26->lock);
  733. cs40l26_pm_exit(cdev);
  734. return error ? error : count;
  735. }
  736. static DEVICE_ATTR_RW(dbc_fall_headroom);
  737. static ssize_t dbc_tx_lvl_thresh_fs_show(struct device *dev, struct device_attribute *attr,
  738. char *buf)
  739. {
  740. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  741. unsigned int val;
  742. int error;
  743. error = cs40l26_dbc_get(cs40l26, CS40L26_DBC_TX_LVL_THRESH_FS, &val);
  744. return error ? error : snprintf(buf, PAGE_SIZE, "%u\n", val);
  745. }
  746. static ssize_t dbc_tx_lvl_thresh_fs_store(struct device *dev,
  747. struct device_attribute *attr, const char *buf, size_t count)
  748. {
  749. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  750. struct device *cdev = cs40l26->dev;
  751. u32 val;
  752. int error;
  753. error = kstrtou32(buf, 10, &val);
  754. if (error)
  755. return error;
  756. error = cs40l26_pm_enter(cdev);
  757. if (error)
  758. return error;
  759. mutex_lock(&cs40l26->lock);
  760. error = cs40l26_dbc_set(cs40l26, CS40L26_DBC_TX_LVL_THRESH_FS, val);
  761. mutex_unlock(&cs40l26->lock);
  762. cs40l26_pm_exit(cdev);
  763. return error ? error : count;
  764. }
  765. static DEVICE_ATTR_RW(dbc_tx_lvl_thresh_fs);
  766. static ssize_t dbc_tx_lvl_hold_off_ms_show(struct device *dev, struct device_attribute *attr,
  767. char *buf)
  768. {
  769. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  770. unsigned int val;
  771. int error;
  772. error = cs40l26_dbc_get(cs40l26, CS40L26_DBC_TX_LVL_HOLD_OFF_MS, &val);
  773. return error ? error : snprintf(buf, PAGE_SIZE, "%u\n", val);
  774. }
  775. static ssize_t dbc_tx_lvl_hold_off_ms_store(struct device *dev,
  776. struct device_attribute *attr, const char *buf, size_t count)
  777. {
  778. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  779. struct device *cdev = cs40l26->dev;
  780. u32 val;
  781. int error;
  782. error = kstrtou32(buf, 10, &val);
  783. if (error)
  784. return error;
  785. error = cs40l26_pm_enter(cdev);
  786. if (error)
  787. return error;
  788. mutex_lock(&cs40l26->lock);
  789. error = cs40l26_dbc_set(cs40l26, CS40L26_DBC_TX_LVL_HOLD_OFF_MS, val);
  790. mutex_unlock(&cs40l26->lock);
  791. cs40l26_pm_exit(cdev);
  792. return error ? error : count;
  793. }
  794. static DEVICE_ATTR_RW(dbc_tx_lvl_hold_off_ms);
  795. static struct attribute *cs40l26_dev_attrs_dbc[] = {
  796. &dev_attr_dbc_enable.attr,
  797. &dev_attr_dbc_env_rel_coef.attr,
  798. &dev_attr_dbc_rise_headroom.attr,
  799. &dev_attr_dbc_fall_headroom.attr,
  800. &dev_attr_dbc_tx_lvl_thresh_fs.attr,
  801. &dev_attr_dbc_tx_lvl_hold_off_ms.attr,
  802. NULL,
  803. };
  804. struct attribute_group cs40l26_dev_attr_dbc_group = {
  805. .name = "dbc",
  806. .attrs = cs40l26_dev_attrs_dbc,
  807. };
  808. static ssize_t trigger_calibration_store(struct device *dev,
  809. struct device_attribute *attr, const char *buf, size_t count)
  810. {
  811. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  812. u32 mailbox_command, calibration_request_payload;
  813. int error;
  814. struct completion *completion;
  815. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  816. dev_info(cs40l26->dev, "%s: %s", __func__, buf);
  817. #else
  818. dev_dbg(cs40l26->dev, "%s: %s", __func__, buf);
  819. #endif
  820. if (!cs40l26->calib_fw) {
  821. dev_err(cs40l26->dev, "Must use calibration firmware\n");
  822. return -EPERM;
  823. }
  824. error = kstrtou32(buf, 16, &calibration_request_payload);
  825. if (error)
  826. return -EINVAL;
  827. switch (calibration_request_payload) {
  828. case CS40L26_CALIBRATION_CONTROL_REQUEST_F0_AND_Q:
  829. completion = &cs40l26->cal_f0_cont;
  830. break;
  831. case CS40L26_CALIBRATION_CONTROL_REQUEST_REDC:
  832. completion = &cs40l26->cal_redc_cont;
  833. break;
  834. case CS40L26_CALIBRATION_CONTROL_REQUEST_DVL_PEQ:
  835. completion = &cs40l26->cal_dvl_peq_cont;
  836. break;
  837. case CS40L26_CALIBRATION_CONTROL_REQUEST_LS_CALIBRATION:
  838. completion = &cs40l26->cal_ls_cont;
  839. break;
  840. default:
  841. return -EINVAL;
  842. }
  843. mailbox_command = ((CS40L26_DSP_MBOX_CMD_INDEX_CALIBRATION_CONTROL <<
  844. CS40L26_DSP_MBOX_CMD_INDEX_SHIFT) & CS40L26_DSP_MBOX_CMD_INDEX_MASK) |
  845. (calibration_request_payload & CS40L26_DSP_MBOX_CMD_PAYLOAD_MASK);
  846. error = cs40l26_pm_enter(cs40l26->dev);
  847. if (error)
  848. return error;
  849. mutex_lock(&cs40l26->lock);
  850. reinit_completion(completion);
  851. error = cs40l26_mailbox_write(cs40l26, mailbox_command);
  852. mutex_unlock(&cs40l26->lock);
  853. if (error) {
  854. dev_err(cs40l26->dev, "Failed to request calibration\n");
  855. goto err_pm;
  856. }
  857. if (!wait_for_completion_timeout(
  858. completion,
  859. msecs_to_jiffies(CS40L26_CALIBRATION_TIMEOUT_MS))) {
  860. error = -ETIME;
  861. dev_err(cs40l26->dev, "Failed to complete cal req, %d, err: %d",
  862. calibration_request_payload, error);
  863. goto err_pm;
  864. }
  865. mutex_lock(&cs40l26->lock);
  866. if (calibration_request_payload == CS40L26_CALIBRATION_CONTROL_REQUEST_F0_AND_Q)
  867. error = cs40l26_copy_f0_est_to_dvl(cs40l26);
  868. mutex_unlock(&cs40l26->lock);
  869. err_pm:
  870. cs40l26_pm_exit(cs40l26->dev);
  871. return error ? error : count;
  872. }
  873. static DEVICE_ATTR_WO(trigger_calibration);
  874. static ssize_t f0_measured_show(struct device *dev, struct device_attribute *attr, char *buf)
  875. {
  876. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  877. u32 reg, f0_measured;
  878. int error;
  879. error = cs40l26_pm_enter(cs40l26->dev);
  880. if (error)
  881. return error;
  882. mutex_lock(&cs40l26->lock);
  883. error = cl_dsp_get_reg(cs40l26->dsp, "F0_EST", CL_DSP_XM_UNPACKED_TYPE,
  884. CS40L26_F0_EST_ALGO_ID, &reg);
  885. if (error)
  886. goto err_mutex;
  887. error = regmap_read(cs40l26->regmap, reg, &f0_measured);
  888. if (error)
  889. goto err_mutex;
  890. err_mutex:
  891. mutex_unlock(&cs40l26->lock);
  892. cs40l26_pm_exit(cs40l26->dev);
  893. if (error)
  894. return error;
  895. else
  896. return snprintf(buf, PAGE_SIZE, "%08X\n", f0_measured);
  897. }
  898. static DEVICE_ATTR_RO(f0_measured);
  899. static ssize_t q_measured_show(struct device *dev, struct device_attribute *attr, char *buf)
  900. {
  901. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  902. u32 reg, q_measured;
  903. int error;
  904. error = cs40l26_pm_enter(cs40l26->dev);
  905. if (error)
  906. return error;
  907. mutex_lock(&cs40l26->lock);
  908. error = cl_dsp_get_reg(cs40l26->dsp, "Q_EST", CL_DSP_XM_UNPACKED_TYPE,
  909. CS40L26_F0_EST_ALGO_ID, &reg);
  910. if (error)
  911. goto err_mutex;
  912. error = regmap_read(cs40l26->regmap, reg, &q_measured);
  913. if (error)
  914. goto err_mutex;
  915. err_mutex:
  916. mutex_unlock(&cs40l26->lock);
  917. cs40l26_pm_exit(cs40l26->dev);
  918. if (error)
  919. return error;
  920. else
  921. return snprintf(buf, PAGE_SIZE, "%08X\n", q_measured);
  922. }
  923. static DEVICE_ATTR_RO(q_measured);
  924. static ssize_t redc_measured_show(struct device *dev, struct device_attribute *attr, char *buf)
  925. {
  926. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  927. u32 reg, redc_measured;
  928. int error;
  929. error = cs40l26_pm_enter(cs40l26->dev);
  930. if (error)
  931. return error;
  932. mutex_lock(&cs40l26->lock);
  933. error = cl_dsp_get_reg(cs40l26->dsp, "RE_EST_STATUS", CL_DSP_YM_UNPACKED_TYPE,
  934. CS40L26_SVC_ALGO_ID, &reg);
  935. if (error)
  936. goto err_mutex;
  937. error = regmap_read(cs40l26->regmap, reg, &redc_measured);
  938. if (error)
  939. goto err_mutex;
  940. err_mutex:
  941. mutex_unlock(&cs40l26->lock);
  942. cs40l26_pm_exit(cs40l26->dev);
  943. if (error)
  944. return error;
  945. else
  946. return snprintf(buf, PAGE_SIZE, "%08X\n", redc_measured);
  947. }
  948. static DEVICE_ATTR_RO(redc_measured);
  949. static ssize_t redc_est_show(struct device *dev, struct device_attribute *attr, char *buf)
  950. {
  951. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  952. u32 reg, redc_est;
  953. int error;
  954. error = cs40l26_pm_enter(cs40l26->dev);
  955. if (error)
  956. return error;
  957. mutex_lock(&cs40l26->lock);
  958. error = cl_dsp_get_reg(cs40l26->dsp, "REDC", CL_DSP_XM_UNPACKED_TYPE,
  959. CS40L26_F0_EST_ALGO_ID, &reg);
  960. if (error)
  961. goto err_mutex;
  962. error = regmap_read(cs40l26->regmap, reg, &redc_est);
  963. if (error)
  964. goto err_mutex;
  965. err_mutex:
  966. mutex_unlock(&cs40l26->lock);
  967. cs40l26_pm_exit(cs40l26->dev);
  968. if (error)
  969. return error;
  970. else
  971. return snprintf(buf, PAGE_SIZE, "%08X\n", redc_est);
  972. }
  973. static ssize_t redc_est_store(struct device *dev, struct device_attribute *attr, const char *buf,
  974. size_t count)
  975. {
  976. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  977. u32 reg, redc_est;
  978. int error;
  979. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  980. dev_info(cs40l26->dev, "%s: %s", __func__, buf);
  981. #else
  982. dev_dbg(cs40l26->dev, "%s: %s", __func__, buf);
  983. #endif
  984. error = kstrtou32(buf, 16, &redc_est);
  985. if (error)
  986. return error;
  987. error = cs40l26_pm_enter(cs40l26->dev);
  988. if (error)
  989. return error;
  990. mutex_lock(&cs40l26->lock);
  991. error = cl_dsp_get_reg(cs40l26->dsp, "REDC", CL_DSP_XM_UNPACKED_TYPE,
  992. CS40L26_F0_EST_ALGO_ID, &reg);
  993. if (error)
  994. goto err_mutex;
  995. error = regmap_write(cs40l26->regmap, reg, redc_est);
  996. if (error)
  997. goto err_mutex;
  998. err_mutex:
  999. mutex_unlock(&cs40l26->lock);
  1000. cs40l26_pm_exit(cs40l26->dev);
  1001. if (error)
  1002. return error;
  1003. else
  1004. return count;
  1005. }
  1006. static DEVICE_ATTR_RW(redc_est);
  1007. static ssize_t f0_stored_show(struct device *dev, struct device_attribute *attr, char *buf)
  1008. {
  1009. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1010. u32 reg, f0_stored;
  1011. int error;
  1012. error = cs40l26_pm_enter(cs40l26->dev);
  1013. if (error)
  1014. return error;
  1015. mutex_lock(&cs40l26->lock);
  1016. error = cl_dsp_get_reg(cs40l26->dsp, "F0_OTP_STORED", CL_DSP_XM_UNPACKED_TYPE,
  1017. CS40L26_VIBEGEN_ALGO_ID, &reg);
  1018. if (error)
  1019. goto err_mutex;
  1020. error = regmap_read(cs40l26->regmap, reg, &f0_stored);
  1021. if (error)
  1022. goto err_mutex;
  1023. err_mutex:
  1024. mutex_unlock(&cs40l26->lock);
  1025. cs40l26_pm_exit(cs40l26->dev);
  1026. if (error)
  1027. return error;
  1028. else
  1029. return snprintf(buf, PAGE_SIZE, "%08X\n", f0_stored);
  1030. }
  1031. static ssize_t f0_stored_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1032. size_t count)
  1033. {
  1034. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1035. u32 reg, f0_stored;
  1036. int error;
  1037. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  1038. dev_info(cs40l26->dev, "%s: %s", __func__, buf);
  1039. #else
  1040. dev_dbg(cs40l26->dev, "%s: %s", __func__, buf);
  1041. #endif
  1042. error = kstrtou32(buf, 16, &f0_stored);
  1043. if (error || f0_stored < CS40L26_F0_EST_MIN || f0_stored > CS40L26_F0_EST_MAX)
  1044. return -EINVAL;
  1045. error = cs40l26_pm_enter(cs40l26->dev);
  1046. if (error)
  1047. return error;
  1048. mutex_lock(&cs40l26->lock);
  1049. error = cl_dsp_get_reg(cs40l26->dsp, "F0_OTP_STORED", CL_DSP_XM_UNPACKED_TYPE,
  1050. CS40L26_VIBEGEN_ALGO_ID, &reg);
  1051. if (error)
  1052. goto err_mutex;
  1053. error = regmap_write(cs40l26->regmap, reg, f0_stored);
  1054. if (error)
  1055. goto err_mutex;
  1056. err_mutex:
  1057. mutex_unlock(&cs40l26->lock);
  1058. cs40l26_pm_exit(cs40l26->dev);
  1059. if (error)
  1060. return error;
  1061. else
  1062. return count;
  1063. }
  1064. static DEVICE_ATTR_RW(f0_stored);
  1065. static ssize_t q_stored_show(struct device *dev, struct device_attribute *attr, char *buf)
  1066. {
  1067. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1068. u32 reg, q_stored;
  1069. int error;
  1070. if (cs40l26->revid == CS40L26_REVID_B2) {
  1071. dev_err(cs40l26->dev, "q_stored not support for revision %02X\n", cs40l26->revid);
  1072. return -EPERM;
  1073. }
  1074. error = cs40l26_pm_enter(cs40l26->dev);
  1075. if (error)
  1076. return error;
  1077. mutex_lock(&cs40l26->lock);
  1078. error = cl_dsp_get_reg(cs40l26->dsp, "Q_STORED", CL_DSP_XM_UNPACKED_TYPE,
  1079. CS40L26_VIBEGEN_ALGO_ID, &reg);
  1080. if (error)
  1081. goto err_mutex;
  1082. error = regmap_read(cs40l26->regmap, reg, &q_stored);
  1083. if (error)
  1084. goto err_mutex;
  1085. err_mutex:
  1086. mutex_unlock(&cs40l26->lock);
  1087. cs40l26_pm_exit(cs40l26->dev);
  1088. if (error)
  1089. return error;
  1090. else
  1091. return snprintf(buf, PAGE_SIZE, "%08X\n", q_stored);
  1092. }
  1093. static ssize_t q_stored_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1094. size_t count)
  1095. {
  1096. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1097. u32 reg, q_stored;
  1098. int error;
  1099. if (cs40l26->revid == CS40L26_REVID_B2) {
  1100. dev_err(cs40l26->dev, "q_stored not support for revision %02X\n", cs40l26->revid);
  1101. return -EPERM;
  1102. }
  1103. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  1104. dev_info(cs40l26->dev, "%s: %s", __func__, buf);
  1105. #else
  1106. dev_dbg(cs40l26->dev, "%s: %s", __func__, buf);
  1107. #endif
  1108. error = kstrtou32(buf, 16, &q_stored);
  1109. if (error || q_stored < CS40L26_Q_EST_MIN || q_stored > CS40L26_Q_EST_MAX)
  1110. return -EINVAL;
  1111. error = cs40l26_pm_enter(cs40l26->dev);
  1112. if (error)
  1113. return error;
  1114. mutex_lock(&cs40l26->lock);
  1115. error = cl_dsp_get_reg(cs40l26->dsp, "Q_STORED", CL_DSP_XM_UNPACKED_TYPE,
  1116. CS40L26_VIBEGEN_ALGO_ID, &reg);
  1117. if (error)
  1118. goto err_mutex;
  1119. error = regmap_write(cs40l26->regmap, reg, q_stored);
  1120. if (error)
  1121. goto err_mutex;
  1122. err_mutex:
  1123. mutex_unlock(&cs40l26->lock);
  1124. cs40l26_pm_exit(cs40l26->dev);
  1125. if (error)
  1126. return error;
  1127. else
  1128. return count;
  1129. }
  1130. static DEVICE_ATTR_RW(q_stored);
  1131. static ssize_t redc_stored_show(struct device *dev, struct device_attribute *attr, char *buf)
  1132. {
  1133. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1134. u32 reg, redc_stored;
  1135. int error;
  1136. error = cs40l26_pm_enter(cs40l26->dev);
  1137. if (error)
  1138. return error;
  1139. mutex_lock(&cs40l26->lock);
  1140. error = cl_dsp_get_reg(cs40l26->dsp, "REDC_OTP_STORED", CL_DSP_XM_UNPACKED_TYPE,
  1141. CS40L26_VIBEGEN_ALGO_ID, &reg);
  1142. if (error)
  1143. goto err_mutex;
  1144. error = regmap_read(cs40l26->regmap, reg, &redc_stored);
  1145. if (error)
  1146. goto err_mutex;
  1147. err_mutex:
  1148. mutex_unlock(&cs40l26->lock);
  1149. cs40l26_pm_exit(cs40l26->dev);
  1150. if (error)
  1151. return error;
  1152. else
  1153. return snprintf(buf, PAGE_SIZE, "%08X\n", redc_stored);
  1154. }
  1155. static ssize_t redc_stored_store(struct device *dev, struct device_attribute *attr,
  1156. const char *buf, size_t count)
  1157. {
  1158. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1159. u32 reg, redc_stored;
  1160. int error;
  1161. #ifdef CONFIG_CS40L26_SAMSUNG_FEATURE
  1162. dev_info(cs40l26->dev, "%s: %s", __func__, buf);
  1163. #else
  1164. dev_dbg(cs40l26->dev, "%s: %s", __func__, buf);
  1165. #endif
  1166. error = kstrtou32(buf, 16, &redc_stored);
  1167. if (error)
  1168. return error;
  1169. error = cs40l26_pm_enter(cs40l26->dev);
  1170. if (error)
  1171. return error;
  1172. mutex_lock(&cs40l26->lock);
  1173. error = cl_dsp_get_reg(cs40l26->dsp, "REDC_OTP_STORED", CL_DSP_XM_UNPACKED_TYPE,
  1174. CS40L26_VIBEGEN_ALGO_ID, &reg);
  1175. if (error)
  1176. goto err_mutex;
  1177. error = regmap_write(cs40l26->regmap, reg, redc_stored);
  1178. if (error)
  1179. goto err_mutex;
  1180. err_mutex:
  1181. mutex_unlock(&cs40l26->lock);
  1182. cs40l26_pm_exit(cs40l26->dev);
  1183. if (error)
  1184. return error;
  1185. else
  1186. return count;
  1187. }
  1188. static DEVICE_ATTR_RW(redc_stored);
  1189. static ssize_t freq_centre_show(struct device *dev, struct device_attribute *attr, char *buf)
  1190. {
  1191. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1192. u32 freq_centre, reg;
  1193. int error;
  1194. error = cs40l26_pm_enter(cs40l26->dev);
  1195. if (error)
  1196. return error;
  1197. mutex_lock(&cs40l26->lock);
  1198. error = cl_dsp_get_reg(cs40l26->dsp, "FREQ_CENTRE", CL_DSP_XM_UNPACKED_TYPE,
  1199. CS40L26_F0_EST_ALGO_ID, &reg);
  1200. if (error)
  1201. goto err_mutex;
  1202. error = regmap_read(cs40l26->regmap, reg, &freq_centre);
  1203. err_mutex:
  1204. mutex_unlock(&cs40l26->lock);
  1205. cs40l26_pm_exit(cs40l26->dev);
  1206. return error ? error : snprintf(buf, PAGE_SIZE, "%08X\n", freq_centre);
  1207. }
  1208. static ssize_t freq_centre_store(struct device *dev, struct device_attribute *attr,
  1209. const char *buf, size_t count)
  1210. {
  1211. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1212. u32 freq_centre, reg;
  1213. int error;
  1214. error = kstrtou32(buf, 16, &freq_centre);
  1215. if (error)
  1216. return error;
  1217. if (freq_centre < CS40L26_F0_FREQ_CENTRE_MIN ||
  1218. freq_centre > CS40L26_F0_FREQ_CENTRE_MAX)
  1219. return -EINVAL;
  1220. error = cs40l26_pm_enter(cs40l26->dev);
  1221. if (error)
  1222. return error;
  1223. mutex_lock(&cs40l26->lock);
  1224. error = cl_dsp_get_reg(cs40l26->dsp, "FREQ_CENTRE", CL_DSP_XM_UNPACKED_TYPE,
  1225. CS40L26_F0_EST_ALGO_ID, &reg);
  1226. if (error)
  1227. goto err_mutex;
  1228. error = regmap_write(cs40l26->regmap, reg, freq_centre);
  1229. err_mutex:
  1230. mutex_unlock(&cs40l26->lock);
  1231. cs40l26_pm_exit(cs40l26->dev);
  1232. return error ? error : count;
  1233. }
  1234. static DEVICE_ATTR_RW(freq_centre);
  1235. static ssize_t freq_span_show(struct device *dev, struct device_attribute *attr, char *buf)
  1236. {
  1237. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1238. int error, freq_span;
  1239. u32 reg;
  1240. error = cs40l26_pm_enter(cs40l26->dev);
  1241. if (error)
  1242. return error;
  1243. mutex_lock(&cs40l26->lock);
  1244. error = cl_dsp_get_reg(cs40l26->dsp, "FREQ_SPAN", CL_DSP_XM_UNPACKED_TYPE,
  1245. CS40L26_F0_EST_ALGO_ID, &reg);
  1246. if (error)
  1247. goto err_mutex;
  1248. error = regmap_read(cs40l26->regmap, reg, &freq_span);
  1249. err_mutex:
  1250. mutex_unlock(&cs40l26->lock);
  1251. cs40l26_pm_exit(cs40l26->dev);
  1252. return error ? error : snprintf(buf, PAGE_SIZE, "%08X\n", freq_span);
  1253. }
  1254. static ssize_t freq_span_store(struct device *dev, struct device_attribute *attr,
  1255. const char *buf, size_t count)
  1256. {
  1257. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1258. int error, s_freq_span;
  1259. u32 freq_span, reg;
  1260. error = kstrtou32(buf, 16, &freq_span);
  1261. if (error)
  1262. return error;
  1263. freq_span &= GENMASK(23, 0);
  1264. s_freq_span = (freq_span & BIT(23)) ? (freq_span | GENMASK(31, 24)) : freq_span;
  1265. if (abs(s_freq_span) < CS40L26_F0_FREQ_SPAN_MIN ||
  1266. abs(s_freq_span) > CS40L26_F0_FREQ_SPAN_MAX)
  1267. return -EINVAL;
  1268. error = cs40l26_pm_enter(cs40l26->dev);
  1269. if (error)
  1270. return error;
  1271. mutex_lock(&cs40l26->lock);
  1272. error = cl_dsp_get_reg(cs40l26->dsp, "FREQ_SPAN", CL_DSP_XM_UNPACKED_TYPE,
  1273. CS40L26_F0_EST_ALGO_ID, &reg);
  1274. if (error)
  1275. goto err_mutex;
  1276. error = regmap_write(cs40l26->regmap, reg, freq_span);
  1277. err_mutex:
  1278. mutex_unlock(&cs40l26->lock);
  1279. cs40l26_pm_exit(cs40l26->dev);
  1280. return error ? error : count;
  1281. }
  1282. static DEVICE_ATTR_RW(freq_span);
  1283. static ssize_t f0_and_q_cal_time_ms_show(struct device *dev, struct device_attribute *attr,
  1284. char *buf)
  1285. {
  1286. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1287. u32 reg, tone_dur_ms, freq_centre, freq_span;
  1288. int error, f0_and_q_cal_time_ms;
  1289. error = cs40l26_pm_enter(cs40l26->dev);
  1290. if (error)
  1291. return error;
  1292. mutex_lock(&cs40l26->lock);
  1293. error = cl_dsp_get_reg(cs40l26->dsp, "TONE_DURATION_MS", CL_DSP_XM_UNPACKED_TYPE,
  1294. CS40L26_F0_EST_ALGO_ID, &reg);
  1295. if (error)
  1296. goto err_mutex;
  1297. error = regmap_read(cs40l26->regmap, reg, &tone_dur_ms);
  1298. if (error) {
  1299. dev_err(cs40l26->dev, "Failed to get tone duration\n");
  1300. goto err_mutex;
  1301. }
  1302. if (tone_dur_ms == 0) { /* Calculate value */
  1303. error = cl_dsp_get_reg(cs40l26->dsp, "FREQ_SPAN", CL_DSP_XM_UNPACKED_TYPE,
  1304. CS40L26_F0_EST_ALGO_ID, &reg);
  1305. if (error)
  1306. goto err_mutex;
  1307. error = regmap_read(cs40l26->regmap, reg, &freq_span);
  1308. if (error) {
  1309. dev_err(cs40l26->dev, "Failed to get FREQ_SPAN\n");
  1310. goto err_mutex;
  1311. }
  1312. error = cl_dsp_get_reg(cs40l26->dsp, "FREQ_CENTRE", CL_DSP_XM_UNPACKED_TYPE,
  1313. CS40L26_F0_EST_ALGO_ID, &reg);
  1314. if (error)
  1315. goto err_mutex;
  1316. error = regmap_read(cs40l26->regmap, reg, &freq_centre);
  1317. if (error) {
  1318. dev_err(cs40l26->dev, "Failed to get FREQ_CENTRE\n");
  1319. goto err_mutex;
  1320. }
  1321. f0_and_q_cal_time_ms = ((CS40L26_F0_CHIRP_DURATION_FACTOR *
  1322. (int) (freq_span >> CS40L26_F0_EST_FREQ_FRAC_BITS)) /
  1323. (int) (freq_centre >> CS40L26_F0_EST_FREQ_FRAC_BITS));
  1324. } else if (tone_dur_ms < CS40L26_F0_AND_Q_CALIBRATION_MIN_MS) {
  1325. f0_and_q_cal_time_ms = CS40L26_F0_AND_Q_CALIBRATION_MIN_MS;
  1326. } else if (tone_dur_ms > CS40L26_F0_AND_Q_CALIBRATION_MAX_MS) {
  1327. f0_and_q_cal_time_ms = CS40L26_F0_AND_Q_CALIBRATION_MAX_MS;
  1328. } else {
  1329. f0_and_q_cal_time_ms = tone_dur_ms;
  1330. }
  1331. err_mutex:
  1332. mutex_unlock(&cs40l26->lock);
  1333. cs40l26_pm_exit(cs40l26->dev);
  1334. if (error)
  1335. return error;
  1336. else
  1337. return snprintf(buf, PAGE_SIZE, "%d\n", f0_and_q_cal_time_ms);
  1338. }
  1339. static DEVICE_ATTR_RO(f0_and_q_cal_time_ms);
  1340. static ssize_t redc_cal_time_ms_show(struct device *dev, struct device_attribute *attr, char *buf)
  1341. {
  1342. /* FIRMWARE_STUMPY_CALIB_REDC_PLAYTIME_MS + SVC_INIT + buffer */
  1343. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1344. u32 reg, redc_playtime_ms, redc_total_cal_time_ms;
  1345. int error;
  1346. error = cs40l26_pm_enter(cs40l26->dev);
  1347. if (error)
  1348. return error;
  1349. mutex_lock(&cs40l26->lock);
  1350. error = cl_dsp_get_reg(cs40l26->dsp, "REDC_PLAYTIME_MS", CL_DSP_XM_UNPACKED_TYPE,
  1351. cs40l26->fw_id, &reg);
  1352. if (error)
  1353. goto err_mutex;
  1354. error = regmap_read(cs40l26->regmap, reg, &redc_playtime_ms);
  1355. redc_total_cal_time_ms = redc_playtime_ms + CS40L26_SVC_INITIALIZATION_PERIOD_MS +
  1356. CS40L26_REDC_CALIBRATION_BUFFER_MS;
  1357. err_mutex:
  1358. mutex_unlock(&cs40l26->lock);
  1359. cs40l26_pm_exit(cs40l26->dev);
  1360. if (error)
  1361. return error;
  1362. else
  1363. return snprintf(buf, PAGE_SIZE, "%d\n", redc_total_cal_time_ms);
  1364. }
  1365. static DEVICE_ATTR_RO(redc_cal_time_ms);
  1366. static ssize_t dvl_peq_coefficients_show(struct device *dev, struct device_attribute *attr,
  1367. char *buf)
  1368. {
  1369. u32 reg, dvl_peq_coefficients[CS40L26_DVL_PEQ_COEFFICIENTS_NUM_REGS];
  1370. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1371. int error;
  1372. error = cs40l26_pm_enter(cs40l26->dev);
  1373. if (error)
  1374. return error;
  1375. mutex_lock(&cs40l26->lock);
  1376. error = cl_dsp_get_reg(cs40l26->dsp, "PEQ_COEF1_X", CL_DSP_XM_UNPACKED_TYPE,
  1377. CS40L26_DVL_ALGO_ID, &reg);
  1378. if (error)
  1379. goto err_mutex;
  1380. error = regmap_bulk_read(cs40l26->regmap, reg, dvl_peq_coefficients,
  1381. CS40L26_DVL_PEQ_COEFFICIENTS_NUM_REGS);
  1382. if (error)
  1383. goto err_mutex;
  1384. err_mutex:
  1385. mutex_unlock(&cs40l26->lock);
  1386. cs40l26_pm_exit(cs40l26->dev);
  1387. if (error)
  1388. return error;
  1389. return snprintf(buf, PAGE_SIZE, "%08X %08X %08X %08X %08X %08X\n",
  1390. dvl_peq_coefficients[0], dvl_peq_coefficients[1], dvl_peq_coefficients[2],
  1391. dvl_peq_coefficients[3], dvl_peq_coefficients[4], dvl_peq_coefficients[5]);
  1392. }
  1393. static ssize_t dvl_peq_coefficients_store(struct device *dev,
  1394. struct device_attribute *attr, const char *buf, size_t count)
  1395. {
  1396. u32 reg, dvl_peq_coefficients[CS40L26_DVL_PEQ_COEFFICIENTS_NUM_REGS];
  1397. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1398. char *coeffs_str, *coeffs_str_temp, *coeff_str;
  1399. int error, coeffs_found = 0;
  1400. coeffs_str = kstrdup(buf, GFP_KERNEL);
  1401. if (!coeffs_str)
  1402. return -ENOMEM;
  1403. coeffs_str_temp = coeffs_str;
  1404. while ((coeff_str = strsep(&coeffs_str_temp, " ")) != NULL) {
  1405. error = kstrtou32(coeff_str, 16, &dvl_peq_coefficients[coeffs_found++]);
  1406. if (error)
  1407. goto err_free;
  1408. }
  1409. if (coeffs_found != CS40L26_DVL_PEQ_COEFFICIENTS_NUM_REGS) {
  1410. dev_err(cs40l26->dev, "Num DVL PEQ coeffs, %d, expecting %d\n",
  1411. coeffs_found, CS40L26_DVL_PEQ_COEFFICIENTS_NUM_REGS);
  1412. error = -EINVAL;
  1413. goto err_free;
  1414. }
  1415. error = cs40l26_pm_enter(cs40l26->dev);
  1416. if (error)
  1417. goto err_free;
  1418. mutex_lock(&cs40l26->lock);
  1419. error = cl_dsp_get_reg(cs40l26->dsp, "PEQ_COEF1_X", CL_DSP_XM_UNPACKED_TYPE,
  1420. CS40L26_DVL_ALGO_ID, &reg);
  1421. if (error)
  1422. goto err_mutex;
  1423. error = regmap_bulk_write(cs40l26->regmap, reg, dvl_peq_coefficients,
  1424. CS40L26_DVL_PEQ_COEFFICIENTS_NUM_REGS);
  1425. if (error)
  1426. dev_err(cs40l26->dev, "Failed to write DVL PEQ coefficients,%d", error);
  1427. err_mutex:
  1428. mutex_unlock(&cs40l26->lock);
  1429. cs40l26_pm_exit(cs40l26->dev);
  1430. err_free:
  1431. kfree(coeffs_str);
  1432. return error ? error : count;
  1433. }
  1434. static DEVICE_ATTR_RW(dvl_peq_coefficients);
  1435. static ssize_t logging_en_show(struct device *dev, struct device_attribute *attr, char *buf)
  1436. {
  1437. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1438. u32 reg, enable;
  1439. int error;
  1440. error = cs40l26_pm_enter(cs40l26->dev);
  1441. if (error)
  1442. return error;
  1443. mutex_lock(&cs40l26->lock);
  1444. error = cl_dsp_get_reg(cs40l26->dsp, "ENABLE", CL_DSP_XM_UNPACKED_TYPE,
  1445. CS40L26_LOGGER_ALGO_ID, &reg);
  1446. if (error)
  1447. goto err_mutex;
  1448. error = regmap_read(cs40l26->regmap, reg, &enable);
  1449. err_mutex:
  1450. mutex_unlock(&cs40l26->lock);
  1451. cs40l26_pm_exit(cs40l26->dev);
  1452. return error ? error : snprintf(buf, PAGE_SIZE, "%u\n", enable);
  1453. }
  1454. static ssize_t logging_en_store(struct device *dev, struct device_attribute *attr, const char *buf,
  1455. size_t count)
  1456. {
  1457. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1458. u32 enable, reg;
  1459. int error;
  1460. error = kstrtou32(buf, 10, &enable);
  1461. if (error)
  1462. return error;
  1463. enable &= CS40L26_LOGGER_EN_MASK;
  1464. error = cs40l26_pm_enter(cs40l26->dev);
  1465. if (error)
  1466. return error;
  1467. mutex_lock(&cs40l26->lock);
  1468. error = cl_dsp_get_reg(cs40l26->dsp, "ENABLE", CL_DSP_XM_UNPACKED_TYPE,
  1469. CS40L26_LOGGER_ALGO_ID, &reg);
  1470. if (error)
  1471. goto err_mutex;
  1472. error = regmap_write(cs40l26->regmap, reg, enable);
  1473. err_mutex:
  1474. mutex_unlock(&cs40l26->lock);
  1475. cs40l26_pm_exit(cs40l26->dev);
  1476. return error ? error : count;
  1477. }
  1478. static DEVICE_ATTR_RW(logging_en);
  1479. static ssize_t logging_max_reset_store(struct device *dev,
  1480. struct device_attribute *attr, const char *buf, size_t count)
  1481. {
  1482. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1483. u32 rst;
  1484. int error;
  1485. error = kstrtou32(buf, 10, &rst);
  1486. if (error)
  1487. return error;
  1488. if (rst != 1)
  1489. return -EINVAL;
  1490. error = cs40l26_pm_enter(cs40l26->dev);
  1491. if (error)
  1492. return error;
  1493. cs40l26_mailbox_write(cs40l26, CS40L26_DSP_MBOX_CMD_LOGGER_MAX_RESET);
  1494. cs40l26_pm_exit(cs40l26->dev);
  1495. return count;
  1496. }
  1497. static DEVICE_ATTR_WO(logging_max_reset);
  1498. static int cs40l26_logger_max_get(struct cs40l26_private *cs40l26, u32 src_id, u32 *max)
  1499. {
  1500. int error, reg, src_num;
  1501. u32 offset;
  1502. error = cs40l26_pm_enter(cs40l26->dev);
  1503. if (error)
  1504. return error;
  1505. mutex_lock(&cs40l26->lock);
  1506. for (src_num = 0; src_num < cs40l26->num_log_srcs; src_num++) {
  1507. if (cs40l26->log_srcs[src_num].id == src_id)
  1508. break;
  1509. }
  1510. if (src_num == cs40l26->num_log_srcs) {
  1511. error = -ENODEV;
  1512. goto err_mutex;
  1513. }
  1514. error = cl_dsp_get_reg(cs40l26->dsp, "DATA", CL_DSP_XM_UNPACKED_TYPE,
  1515. CS40L26_LOGGER_ALGO_ID, &reg);
  1516. if (error)
  1517. goto err_mutex;
  1518. offset = (src_num * CS40L26_LOGGER_DATA_MAX_STEP) + CS40L26_LOGGER_DATA_MAX_OFFSET;
  1519. error = regmap_read(cs40l26->regmap, reg + offset, max);
  1520. err_mutex:
  1521. mutex_unlock(&cs40l26->lock);
  1522. cs40l26_pm_exit(cs40l26->dev);
  1523. return error;
  1524. }
  1525. static ssize_t max_bemf_show(struct device *dev, struct device_attribute *attr, char *buf)
  1526. {
  1527. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1528. u32 max_bemf;
  1529. int error;
  1530. error = cs40l26_logger_max_get(cs40l26, CS40L26_LOGGER_SRC_ID_BEMF, &max_bemf);
  1531. return error ? error : snprintf(buf, PAGE_SIZE, "0x%06X\n", max_bemf);
  1532. }
  1533. static DEVICE_ATTR_RO(max_bemf);
  1534. static ssize_t max_vbst_show(struct device *dev, struct device_attribute *attr, char *buf)
  1535. {
  1536. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1537. u32 max_vbst;
  1538. int error;
  1539. error = cs40l26_logger_max_get(cs40l26, CS40L26_LOGGER_SRC_ID_VBST, &max_vbst);
  1540. return error ? error : snprintf(buf, PAGE_SIZE, "0x%06X\n", max_vbst);
  1541. }
  1542. static DEVICE_ATTR_RO(max_vbst);
  1543. static ssize_t max_vmon_show(struct device *dev, struct device_attribute *attr, char *buf)
  1544. {
  1545. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1546. u32 max_vmon;
  1547. int error;
  1548. error = cs40l26_logger_max_get(cs40l26, CS40L26_LOGGER_SRC_ID_VMON, &max_vmon);
  1549. return error ? error : snprintf(buf, PAGE_SIZE, "0x%06X\n", max_vmon);
  1550. }
  1551. static DEVICE_ATTR_RO(max_vmon);
  1552. static ssize_t max_excursion_show(struct device *dev, struct device_attribute *attr, char *buf)
  1553. {
  1554. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1555. u32 max_excursion;
  1556. int error;
  1557. error = cs40l26_logger_max_get(cs40l26, CS40L26_LOGGER_SRC_ID_EP, &max_excursion);
  1558. return error ? error : snprintf(buf, PAGE_SIZE, "0x%06X\n", max_excursion);
  1559. }
  1560. static DEVICE_ATTR_RO(max_excursion);
  1561. static ssize_t svc_le_est_show(struct device *dev, struct device_attribute *attr, char *buf)
  1562. {
  1563. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1564. unsigned int le;
  1565. int error;
  1566. error = cs40l26_pm_enter(cs40l26->dev);
  1567. if (error)
  1568. return error;
  1569. mutex_lock(&cs40l26->lock);
  1570. error = cs40l26_svc_le_estimate(cs40l26, &le);
  1571. mutex_unlock(&cs40l26->lock);
  1572. cs40l26_pm_exit(cs40l26->dev);
  1573. if (error)
  1574. return error;
  1575. return snprintf(buf, PAGE_SIZE, "%u\n", le);
  1576. }
  1577. static DEVICE_ATTR_RO(svc_le_est);
  1578. static ssize_t svc_le_stored_show(struct device *dev, struct device_attribute *attr, char *buf)
  1579. {
  1580. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1581. int error;
  1582. mutex_lock(&cs40l26->lock);
  1583. error = snprintf(buf, PAGE_SIZE, "%d\n", cs40l26->svc_le_est_stored);
  1584. mutex_unlock(&cs40l26->lock);
  1585. return error;
  1586. }
  1587. static ssize_t svc_le_stored_store(struct device *dev,
  1588. struct device_attribute *attr, const char *buf, size_t count)
  1589. {
  1590. struct cs40l26_private *cs40l26 = dev_get_drvdata(dev);
  1591. u32 svc_le_stored;
  1592. int error;
  1593. error = kstrtou32(buf, 10, &svc_le_stored);
  1594. if (error)
  1595. return error;
  1596. mutex_lock(&cs40l26->lock);
  1597. cs40l26->svc_le_est_stored = svc_le_stored;
  1598. mutex_unlock(&cs40l26->lock);
  1599. return count;
  1600. }
  1601. static DEVICE_ATTR_RW(svc_le_stored);
  1602. static struct attribute *cs40l26_dev_attrs_cal[] = {
  1603. &dev_attr_svc_le_est.attr,
  1604. &dev_attr_svc_le_stored.attr,
  1605. &dev_attr_max_vbst.attr,
  1606. &dev_attr_max_bemf.attr,
  1607. &dev_attr_max_vmon.attr,
  1608. &dev_attr_max_excursion.attr,
  1609. &dev_attr_logging_max_reset.attr,
  1610. &dev_attr_logging_en.attr,
  1611. &dev_attr_trigger_calibration.attr,
  1612. &dev_attr_f0_measured.attr,
  1613. &dev_attr_q_measured.attr,
  1614. &dev_attr_redc_measured.attr,
  1615. &dev_attr_dvl_peq_coefficients.attr,
  1616. &dev_attr_redc_est.attr,
  1617. &dev_attr_f0_stored.attr,
  1618. &dev_attr_q_stored.attr,
  1619. &dev_attr_redc_stored.attr,
  1620. &dev_attr_freq_centre.attr,
  1621. &dev_attr_freq_span.attr,
  1622. &dev_attr_f0_and_q_cal_time_ms.attr,
  1623. &dev_attr_redc_cal_time_ms.attr,
  1624. NULL,
  1625. };
  1626. struct attribute_group cs40l26_dev_attr_cal_group = {
  1627. .name = "calibration",
  1628. .attrs = cs40l26_dev_attrs_cal,
  1629. };