audio_acdb.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  1. /* Copyright (c) 2010-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/slab.h>
  14. #include <linux/fs.h>
  15. #include <linux/module.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/mutex.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/msm_ion.h>
  20. #include <linux/mm.h>
  21. #include <linux/msm_audio_ion.h>
  22. #include "audio_acdb.h"
  23. #include "q6voice.h"
  24. #include <sound/q6adm-v2.h>
  25. #include <sound/q6afe-v2.h>
  26. #include <sound/q6asm-v2.h>
  27. #include <sound/q6lsm.h>
  28. #define MAX_NETWORKS 15
  29. #define MAX_IOCTL_DATA (MAX_NETWORKS * 2)
  30. #define MAX_COL_SIZE 324
  31. #define ACDB_BLOCK_SIZE 4096
  32. #define NUM_VOCPROC_BLOCKS (6 * MAX_NETWORKS)
  33. #define ACDB_TOTAL_VOICE_ALLOCATION (ACDB_BLOCK_SIZE * NUM_VOCPROC_BLOCKS)
  34. #define MAX_HW_DELAY_ENTRIES 25
  35. struct acdb_data {
  36. uint32_t usage_count;
  37. struct mutex acdb_mutex;
  38. /* ANC Cal */
  39. struct acdb_cal_block anc_cal;
  40. /* AANC Cal */
  41. struct acdb_cal_block aanc_cal;
  42. /* LSM Cal */
  43. struct acdb_cal_block lsm_cal;
  44. /* AudProc Cal */
  45. uint32_t asm_topology;
  46. uint32_t adm_topology[MAX_AUDPROC_TYPES];
  47. struct acdb_cal_block audproc_cal[MAX_AUDPROC_TYPES];
  48. struct acdb_cal_block audstrm_cal[MAX_AUDPROC_TYPES];
  49. struct acdb_cal_block audvol_cal[MAX_AUDPROC_TYPES];
  50. /* VocProc Cal */
  51. uint32_t voice_rx_topology;
  52. uint32_t voice_tx_topology;
  53. struct acdb_cal_block vocproc_cal;
  54. struct acdb_cal_block vocstrm_cal;
  55. struct acdb_cal_block vocvol_cal;
  56. /* Voice Column data */
  57. struct acdb_cal_block vocproc_col_cal[MAX_VOCPROC_TYPES];
  58. uint32_t *col_data[MAX_VOCPROC_TYPES];
  59. /* VocProc dev cfg cal*/
  60. struct acdb_cal_block vocproc_dev_cal;
  61. /* Custom topology */
  62. struct acdb_cal_block adm_custom_topology;
  63. struct acdb_cal_block asm_custom_topology;
  64. uint32_t valid_adm_custom_top;
  65. uint32_t valid_asm_custom_top;
  66. /* AFE cal */
  67. struct acdb_cal_block afe_cal[MAX_AUDPROC_TYPES];
  68. /* Sidetone Cal */
  69. struct sidetone_cal sidetone_cal;
  70. /* Allocation information */
  71. struct ion_client *ion_client;
  72. struct ion_handle *ion_handle;
  73. uint32_t map_handle;
  74. uint64_t paddr;
  75. uint64_t kvaddr;
  76. uint64_t mem_len;
  77. /* Speaker protection */
  78. struct msm_spk_prot_cfg spk_prot_cfg;
  79. /* Av sync delay info */
  80. struct hw_delay hw_delay_rx;
  81. struct hw_delay hw_delay_tx;
  82. };
  83. static struct acdb_data acdb_data;
  84. uint32_t get_voice_rx_topology(void)
  85. {
  86. return acdb_data.voice_rx_topology;
  87. }
  88. void store_voice_rx_topology(uint32_t topology)
  89. {
  90. acdb_data.voice_rx_topology = topology;
  91. }
  92. uint32_t get_voice_tx_topology(void)
  93. {
  94. return acdb_data.voice_tx_topology;
  95. }
  96. void store_voice_tx_topology(uint32_t topology)
  97. {
  98. acdb_data.voice_tx_topology = topology;
  99. }
  100. uint32_t get_adm_rx_topology(void)
  101. {
  102. return acdb_data.adm_topology[RX_CAL];
  103. }
  104. void store_adm_rx_topology(uint32_t topology)
  105. {
  106. acdb_data.adm_topology[RX_CAL] = topology;
  107. }
  108. uint32_t get_adm_tx_topology(void)
  109. {
  110. return acdb_data.adm_topology[TX_CAL];
  111. }
  112. void store_adm_tx_topology(uint32_t topology)
  113. {
  114. acdb_data.adm_topology[TX_CAL] = topology;
  115. }
  116. uint32_t get_asm_topology(void)
  117. {
  118. return acdb_data.asm_topology;
  119. }
  120. void store_asm_topology(uint32_t topology)
  121. {
  122. acdb_data.asm_topology = topology;
  123. }
  124. void reset_custom_topology_flags(void)
  125. {
  126. mutex_lock(&acdb_data.acdb_mutex);
  127. acdb_data.valid_adm_custom_top = 1;
  128. acdb_data.valid_asm_custom_top = 1;
  129. mutex_unlock(&acdb_data.acdb_mutex);
  130. }
  131. int get_adm_custom_topology(struct acdb_cal_block *cal_block)
  132. {
  133. int result = 0;
  134. pr_debug("%s\n", __func__);
  135. if (cal_block == NULL) {
  136. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  137. result = -EINVAL;
  138. goto done;
  139. }
  140. mutex_lock(&acdb_data.acdb_mutex);
  141. /* Only return allow one access after memory registered */
  142. if (acdb_data.valid_adm_custom_top == 0) {
  143. cal_block->cal_size = 0;
  144. goto unlock;
  145. }
  146. acdb_data.valid_adm_custom_top = 0;
  147. cal_block->cal_size = acdb_data.adm_custom_topology.cal_size;
  148. cal_block->cal_paddr = acdb_data.adm_custom_topology.cal_paddr;
  149. cal_block->cal_kvaddr = acdb_data.adm_custom_topology.cal_kvaddr;
  150. unlock:
  151. mutex_unlock(&acdb_data.acdb_mutex);
  152. done:
  153. return result;
  154. }
  155. int store_adm_custom_topology(struct cal_block *cal_block)
  156. {
  157. int result = 0;
  158. pr_debug("%s,\n", __func__);
  159. if (cal_block->cal_offset > acdb_data.mem_len) {
  160. pr_err("%s: offset %d is > mem_len %llu\n",
  161. __func__, cal_block->cal_offset, acdb_data.mem_len);
  162. result = -EINVAL;
  163. goto done;
  164. }
  165. acdb_data.adm_custom_topology.cal_size = cal_block->cal_size;
  166. acdb_data.adm_custom_topology.cal_paddr =
  167. cal_block->cal_offset + acdb_data.paddr;
  168. acdb_data.adm_custom_topology.cal_kvaddr =
  169. cal_block->cal_offset + acdb_data.kvaddr;
  170. done:
  171. return result;
  172. }
  173. int get_asm_custom_topology(struct acdb_cal_block *cal_block)
  174. {
  175. int result = 0;
  176. pr_debug("%s,\n", __func__);
  177. if (cal_block == NULL) {
  178. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  179. result = -EINVAL;
  180. goto done;
  181. }
  182. mutex_lock(&acdb_data.acdb_mutex);
  183. /* Only return allow one access after memory registered */
  184. if (acdb_data.valid_asm_custom_top == 0) {
  185. cal_block->cal_size = 0;
  186. goto unlock;
  187. }
  188. acdb_data.valid_asm_custom_top = 0;
  189. cal_block->cal_size = acdb_data.asm_custom_topology.cal_size;
  190. cal_block->cal_paddr = acdb_data.asm_custom_topology.cal_paddr;
  191. cal_block->cal_kvaddr = acdb_data.asm_custom_topology.cal_kvaddr;
  192. unlock:
  193. mutex_unlock(&acdb_data.acdb_mutex);
  194. done:
  195. return result;
  196. }
  197. int store_asm_custom_topology(struct cal_block *cal_block)
  198. {
  199. int result = 0;
  200. pr_debug("%s,\n", __func__);
  201. if (cal_block->cal_offset > acdb_data.mem_len) {
  202. pr_err("%s: offset %d is > mem_len %llu\n",
  203. __func__, cal_block->cal_offset, acdb_data.mem_len);
  204. result = -EINVAL;
  205. goto done;
  206. }
  207. acdb_data.asm_custom_topology.cal_size = cal_block->cal_size;
  208. acdb_data.asm_custom_topology.cal_paddr =
  209. cal_block->cal_offset + acdb_data.paddr;
  210. acdb_data.asm_custom_topology.cal_kvaddr =
  211. cal_block->cal_offset + acdb_data.kvaddr;
  212. done:
  213. return result;
  214. }
  215. int get_voice_cal_allocation(struct acdb_cal_block *cal_block)
  216. {
  217. int result = 0;
  218. pr_debug("%s,\n", __func__);
  219. if (cal_block == NULL) {
  220. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  221. result = -EINVAL;
  222. goto done;
  223. }
  224. cal_block->cal_size = ACDB_TOTAL_VOICE_ALLOCATION;
  225. cal_block->cal_paddr = acdb_data.vocproc_cal.cal_paddr;
  226. cal_block->cal_kvaddr = acdb_data.vocproc_cal.cal_kvaddr;
  227. done:
  228. return result;
  229. }
  230. int get_aanc_cal(struct acdb_cal_block *cal_block)
  231. {
  232. int result = 0;
  233. pr_debug("%s,\n", __func__);
  234. if (cal_block == NULL) {
  235. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  236. result = -EINVAL;
  237. goto done;
  238. }
  239. cal_block->cal_size = acdb_data.aanc_cal.cal_size;
  240. cal_block->cal_paddr = acdb_data.aanc_cal.cal_paddr;
  241. cal_block->cal_kvaddr = acdb_data.aanc_cal.cal_kvaddr;
  242. done:
  243. return result;
  244. }
  245. int store_aanc_cal(struct cal_block *cal_block)
  246. {
  247. int result = 0;
  248. pr_debug("%s,\n", __func__);
  249. if (cal_block->cal_offset > acdb_data.mem_len) {
  250. pr_err("%s: offset %d is > mem_len %llu\n",
  251. __func__, cal_block->cal_offset, acdb_data.mem_len);
  252. result = -EINVAL;
  253. goto done;
  254. }
  255. acdb_data.aanc_cal.cal_size = cal_block->cal_size;
  256. acdb_data.aanc_cal.cal_paddr =
  257. cal_block->cal_offset + acdb_data.paddr;
  258. acdb_data.aanc_cal.cal_kvaddr =
  259. cal_block->cal_offset + acdb_data.kvaddr;
  260. done:
  261. return result;
  262. }
  263. int get_lsm_cal(struct acdb_cal_block *cal_block)
  264. {
  265. int result = 0;
  266. pr_debug("%s,\n", __func__);
  267. if (cal_block == NULL) {
  268. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  269. result = -EINVAL;
  270. goto done;
  271. }
  272. cal_block->cal_size = acdb_data.lsm_cal.cal_size;
  273. cal_block->cal_paddr = acdb_data.lsm_cal.cal_paddr;
  274. cal_block->cal_kvaddr = acdb_data.lsm_cal.cal_kvaddr;
  275. done:
  276. return result;
  277. }
  278. int store_lsm_cal(struct cal_block *cal_block)
  279. {
  280. int result = 0;
  281. pr_debug("%s,\n", __func__);
  282. if (cal_block->cal_offset > acdb_data.mem_len) {
  283. pr_err("%s: offset %d is > mem_len %llu\n",
  284. __func__, cal_block->cal_offset, acdb_data.mem_len);
  285. result = -EINVAL;
  286. goto done;
  287. }
  288. acdb_data.lsm_cal.cal_size = cal_block->cal_size;
  289. acdb_data.lsm_cal.cal_paddr =
  290. cal_block->cal_offset + acdb_data.paddr;
  291. acdb_data.lsm_cal.cal_kvaddr =
  292. cal_block->cal_offset + acdb_data.kvaddr;
  293. done:
  294. return result;
  295. }
  296. int get_hw_delay(int32_t path, struct hw_delay_entry *entry)
  297. {
  298. int i, result = 0;
  299. struct hw_delay *delay = NULL;
  300. struct hw_delay_entry *info = NULL;
  301. pr_debug("%s,\n", __func__);
  302. if (entry == NULL) {
  303. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  304. result = -EINVAL;
  305. goto ret;
  306. }
  307. if ((path >= MAX_AUDPROC_TYPES) || (path < 0)) {
  308. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  309. __func__, path);
  310. result = -EINVAL;
  311. goto ret;
  312. }
  313. mutex_lock(&acdb_data.acdb_mutex);
  314. if (path == RX_CAL)
  315. delay = &acdb_data.hw_delay_rx;
  316. else if (path == TX_CAL)
  317. delay = &acdb_data.hw_delay_tx;
  318. if ((delay == NULL) || ((delay != NULL) && delay->num_entries == 0)) {
  319. pr_debug("ACDB=> %s Invalid delay/ delay entries\n", __func__);
  320. result = -EINVAL;
  321. goto done;
  322. }
  323. info = (struct hw_delay_entry *)(delay->delay_info);
  324. if (info == NULL) {
  325. pr_err("ACDB=> %s Delay entries info is NULL\n", __func__);
  326. result = -EFAULT;
  327. goto done;
  328. }
  329. for (i = 0; i < delay->num_entries; i++) {
  330. if (info[i].sample_rate == entry->sample_rate) {
  331. entry->delay_usec = info[i].delay_usec;
  332. break;
  333. }
  334. }
  335. if (i == delay->num_entries) {
  336. pr_err("ACDB=> %s: Unable to find delay for sample rate %d\n",
  337. __func__, entry->sample_rate);
  338. result = -EFAULT;
  339. }
  340. done:
  341. mutex_unlock(&acdb_data.acdb_mutex);
  342. pr_debug("ACDB=> %s: Path = %d samplerate = %u usec = %u status %d\n",
  343. __func__, path, entry->sample_rate, entry->delay_usec, result);
  344. ret:
  345. return result;
  346. }
  347. int store_hw_delay(int32_t path, void *arg)
  348. {
  349. int result = 0;
  350. struct hw_delay delay;
  351. struct hw_delay *delay_dest = NULL;
  352. pr_debug("%s,\n", __func__);
  353. if ((path >= MAX_AUDPROC_TYPES) || (path < 0) || (arg == NULL)) {
  354. pr_err("ACDB=> Bad path/ pointer sent to %s, path: %d\n",
  355. __func__, path);
  356. result = -EINVAL;
  357. goto done;
  358. }
  359. result = copy_from_user((void *)&delay, (void *)arg,
  360. sizeof(struct hw_delay));
  361. if (result) {
  362. pr_err("ACDB=> %s failed to copy hw delay: result=%d path=%d\n",
  363. __func__, result, path);
  364. result = -EFAULT;
  365. goto done;
  366. }
  367. if ((delay.num_entries <= 0) ||
  368. (delay.num_entries > MAX_HW_DELAY_ENTRIES)) {
  369. pr_debug("ACDB=> %s incorrect no of hw delay entries: %d\n",
  370. __func__, delay.num_entries);
  371. result = -EINVAL;
  372. goto done;
  373. }
  374. if ((path >= MAX_AUDPROC_TYPES) || (path < 0)) {
  375. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  376. __func__, path);
  377. result = -EINVAL;
  378. goto done;
  379. }
  380. pr_debug("ACDB=> %s : Path = %d num_entries = %d\n",
  381. __func__, path, delay.num_entries);
  382. if (path == RX_CAL)
  383. delay_dest = &acdb_data.hw_delay_rx;
  384. else if (path == TX_CAL)
  385. delay_dest = &acdb_data.hw_delay_tx;
  386. delay_dest->num_entries = delay.num_entries;
  387. result = copy_from_user(delay_dest->delay_info,
  388. delay.delay_info,
  389. (sizeof(struct hw_delay_entry)*
  390. delay.num_entries));
  391. if (result) {
  392. pr_err("ACDB=> %s failed to copy hw delay info res=%d path=%d",
  393. __func__, result, path);
  394. result = -EFAULT;
  395. }
  396. done:
  397. return result;
  398. }
  399. int get_anc_cal(struct acdb_cal_block *cal_block)
  400. {
  401. int result = 0;
  402. pr_debug("%s,\n", __func__);
  403. if (cal_block == NULL) {
  404. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  405. result = -EINVAL;
  406. goto done;
  407. }
  408. cal_block->cal_size = acdb_data.anc_cal.cal_size;
  409. cal_block->cal_paddr = acdb_data.anc_cal.cal_paddr;
  410. cal_block->cal_kvaddr = acdb_data.anc_cal.cal_kvaddr;
  411. done:
  412. return result;
  413. }
  414. int store_anc_cal(struct cal_block *cal_block)
  415. {
  416. int result = 0;
  417. pr_debug("%s,\n", __func__);
  418. if (cal_block->cal_offset > acdb_data.mem_len) {
  419. pr_err("%s: offset %d is > mem_len %llu\n",
  420. __func__, cal_block->cal_offset, acdb_data.mem_len);
  421. result = -EINVAL;
  422. goto done;
  423. }
  424. acdb_data.anc_cal.cal_size = cal_block->cal_size;
  425. acdb_data.anc_cal.cal_paddr =
  426. cal_block->cal_offset + acdb_data.paddr;
  427. acdb_data.anc_cal.cal_kvaddr =
  428. cal_block->cal_offset + acdb_data.kvaddr;
  429. done:
  430. return result;
  431. }
  432. int store_afe_cal(int32_t path, struct cal_block *cal_block)
  433. {
  434. int result = 0;
  435. pr_debug("%s, path = %d\n", __func__, path);
  436. if (cal_block->cal_offset > acdb_data.mem_len) {
  437. pr_err("%s: offset %d is > mem_len %llu\n",
  438. __func__, cal_block->cal_offset, acdb_data.mem_len);
  439. result = -EINVAL;
  440. goto done;
  441. }
  442. if ((path >= MAX_AUDPROC_TYPES) || (path < 0)) {
  443. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  444. __func__, path);
  445. result = -EINVAL;
  446. goto done;
  447. }
  448. acdb_data.afe_cal[path].cal_size = cal_block->cal_size;
  449. acdb_data.afe_cal[path].cal_paddr =
  450. cal_block->cal_offset + acdb_data.paddr;
  451. acdb_data.afe_cal[path].cal_kvaddr =
  452. cal_block->cal_offset + acdb_data.kvaddr;
  453. done:
  454. return result;
  455. }
  456. int get_afe_cal(int32_t path, struct acdb_cal_block *cal_block)
  457. {
  458. int result = 0;
  459. pr_debug("%s, path = %d\n", __func__, path);
  460. if (cal_block == NULL) {
  461. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  462. result = -EINVAL;
  463. goto done;
  464. }
  465. if ((path >= MAX_AUDPROC_TYPES) || (path < 0)) {
  466. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  467. __func__, path);
  468. result = -EINVAL;
  469. goto done;
  470. }
  471. cal_block->cal_size = acdb_data.afe_cal[path].cal_size;
  472. cal_block->cal_paddr = acdb_data.afe_cal[path].cal_paddr;
  473. cal_block->cal_kvaddr = acdb_data.afe_cal[path].cal_kvaddr;
  474. done:
  475. return result;
  476. }
  477. int store_audproc_cal(int32_t path, struct cal_block *cal_block)
  478. {
  479. int result = 0;
  480. pr_debug("%s, path = %d\n", __func__, path);
  481. if (cal_block->cal_offset > acdb_data.mem_len) {
  482. pr_err("%s: offset %d is > mem_len %llu\n",
  483. __func__, cal_block->cal_offset, acdb_data.mem_len);
  484. result = -EINVAL;
  485. goto done;
  486. }
  487. if (path >= MAX_AUDPROC_TYPES) {
  488. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  489. __func__, path);
  490. result = -EINVAL;
  491. goto done;
  492. }
  493. acdb_data.audproc_cal[path].cal_size = cal_block->cal_size;
  494. acdb_data.audproc_cal[path].cal_paddr =
  495. cal_block->cal_offset + acdb_data.paddr;
  496. acdb_data.audproc_cal[path].cal_kvaddr =
  497. cal_block->cal_offset + acdb_data.kvaddr;
  498. done:
  499. return result;
  500. }
  501. int get_audproc_cal(int32_t path, struct acdb_cal_block *cal_block)
  502. {
  503. int result = 0;
  504. pr_debug("%s, path = %d\n", __func__, path);
  505. if (cal_block == NULL) {
  506. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  507. result = -EINVAL;
  508. goto done;
  509. }
  510. if (path >= MAX_AUDPROC_TYPES) {
  511. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  512. __func__, path);
  513. result = -EINVAL;
  514. goto done;
  515. }
  516. cal_block->cal_size = acdb_data.audproc_cal[path].cal_size;
  517. cal_block->cal_paddr = acdb_data.audproc_cal[path].cal_paddr;
  518. cal_block->cal_kvaddr = acdb_data.audproc_cal[path].cal_kvaddr;
  519. done:
  520. return result;
  521. }
  522. int store_audstrm_cal(int32_t path, struct cal_block *cal_block)
  523. {
  524. int result = 0;
  525. pr_debug("%s, path = %d\n", __func__, path);
  526. if (cal_block->cal_offset > acdb_data.mem_len) {
  527. pr_err("%s: offset %d is > mem_len %llu\n",
  528. __func__, cal_block->cal_offset, acdb_data.mem_len);
  529. result = -EINVAL;
  530. goto done;
  531. }
  532. if (path >= MAX_AUDPROC_TYPES) {
  533. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  534. __func__, path);
  535. result = -EINVAL;
  536. goto done;
  537. }
  538. acdb_data.audstrm_cal[path].cal_size = cal_block->cal_size;
  539. acdb_data.audstrm_cal[path].cal_paddr =
  540. cal_block->cal_offset + acdb_data.paddr;
  541. acdb_data.audstrm_cal[path].cal_kvaddr =
  542. cal_block->cal_offset + acdb_data.kvaddr;
  543. done:
  544. return result;
  545. }
  546. int get_audstrm_cal(int32_t path, struct acdb_cal_block *cal_block)
  547. {
  548. int result = 0;
  549. pr_debug("%s, path = %d\n", __func__, path);
  550. if (cal_block == NULL) {
  551. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  552. result = -EINVAL;
  553. goto done;
  554. }
  555. if (path >= MAX_AUDPROC_TYPES) {
  556. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  557. __func__, path);
  558. result = -EINVAL;
  559. goto done;
  560. }
  561. cal_block->cal_size = acdb_data.audstrm_cal[path].cal_size;
  562. cal_block->cal_paddr = acdb_data.audstrm_cal[path].cal_paddr;
  563. cal_block->cal_kvaddr = acdb_data.audstrm_cal[path].cal_kvaddr;
  564. done:
  565. return result;
  566. }
  567. int store_audvol_cal(int32_t path, struct cal_block *cal_block)
  568. {
  569. int result = 0;
  570. pr_debug("%s, path = %d\n", __func__, path);
  571. if (cal_block->cal_offset > acdb_data.mem_len) {
  572. pr_err("%s: offset %d is > mem_len %llu\n",
  573. __func__, cal_block->cal_offset, acdb_data.mem_len);
  574. result = -EINVAL;
  575. goto done;
  576. }
  577. if (path >= MAX_AUDPROC_TYPES) {
  578. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  579. __func__, path);
  580. result = -EINVAL;
  581. goto done;
  582. }
  583. acdb_data.audvol_cal[path].cal_size = cal_block->cal_size;
  584. acdb_data.audvol_cal[path].cal_paddr =
  585. cal_block->cal_offset + acdb_data.paddr;
  586. acdb_data.audvol_cal[path].cal_kvaddr =
  587. cal_block->cal_offset + acdb_data.kvaddr;
  588. done:
  589. return result;
  590. }
  591. int get_audvol_cal(int32_t path, struct acdb_cal_block *cal_block)
  592. {
  593. int result = 0;
  594. pr_debug("%s, path = %d\n", __func__, path);
  595. if (cal_block == NULL) {
  596. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  597. result = -EINVAL;
  598. goto done;
  599. }
  600. if (path >= MAX_AUDPROC_TYPES || path < 0) {
  601. pr_err("ACDB=> Bad path sent to %s, path: %d\n",
  602. __func__, path);
  603. result = -EINVAL;
  604. goto done;
  605. }
  606. cal_block->cal_size = acdb_data.audvol_cal[path].cal_size;
  607. cal_block->cal_paddr = acdb_data.audvol_cal[path].cal_paddr;
  608. cal_block->cal_kvaddr = acdb_data.audvol_cal[path].cal_kvaddr;
  609. done:
  610. return result;
  611. }
  612. int store_voice_col_data(uint32_t vocproc_type, uint32_t cal_size,
  613. uint32_t *cal_block)
  614. {
  615. int result = 0;
  616. pr_debug("%s,\n", __func__);
  617. if (cal_block == NULL) {
  618. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  619. result = -EINVAL;
  620. goto done;
  621. }
  622. if (cal_size > MAX_COL_SIZE) {
  623. pr_err("%s: col size is to big %d\n", __func__, cal_size);
  624. result = -EINVAL;
  625. goto done;
  626. }
  627. if (acdb_data.col_data[vocproc_type] == NULL) {
  628. pr_err("%s: vocproc_type %d data not allocated!\n",
  629. __func__, vocproc_type);
  630. result = -EINVAL;
  631. goto done;
  632. }
  633. if (copy_from_user(acdb_data.col_data[vocproc_type],
  634. (void *)((uint8_t *)cal_block + sizeof(cal_size)),
  635. cal_size)) {
  636. pr_err("%s: fail to copy col size %d\n", __func__, cal_size);
  637. result = -EINVAL;
  638. goto done;
  639. }
  640. acdb_data.vocproc_col_cal[vocproc_type].cal_size = cal_size;
  641. done:
  642. return result;
  643. }
  644. int get_voice_col_data(uint32_t vocproc_type,
  645. struct acdb_cal_block *cal_block)
  646. {
  647. int result = 0;
  648. pr_debug("%s,\n", __func__);
  649. if (cal_block == NULL) {
  650. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  651. result = -EINVAL;
  652. goto done;
  653. }
  654. if (acdb_data.col_data[vocproc_type] == NULL) {
  655. pr_err("%s: vocproc_type %d data not allocated!\n",
  656. __func__, vocproc_type);
  657. result = -EINVAL;
  658. goto done;
  659. }
  660. cal_block->cal_size = acdb_data.
  661. vocproc_col_cal[vocproc_type].cal_size;
  662. cal_block->cal_paddr = acdb_data.
  663. vocproc_col_cal[vocproc_type].cal_paddr;
  664. cal_block->cal_kvaddr = acdb_data.
  665. vocproc_col_cal[vocproc_type].cal_kvaddr;
  666. done:
  667. return result;
  668. }
  669. int store_vocproc_dev_cfg_cal(struct cal_block *cal_block)
  670. {
  671. int result = 0;
  672. pr_debug("%s,\n", __func__);
  673. if (cal_block->cal_offset >
  674. acdb_data.mem_len) {
  675. pr_err("%s: offset %d is > mem_len %llu\n",
  676. __func__, cal_block->cal_offset, acdb_data.mem_len);
  677. acdb_data.vocproc_dev_cal.cal_size = 0;
  678. result = -EINVAL;
  679. goto done;
  680. }
  681. acdb_data.vocproc_dev_cal.cal_size = cal_block->cal_size;
  682. acdb_data.vocproc_dev_cal.cal_paddr =
  683. cal_block->cal_offset + acdb_data.paddr;
  684. acdb_data.vocproc_dev_cal.cal_kvaddr =
  685. cal_block->cal_offset + acdb_data.kvaddr;
  686. done:
  687. return result;
  688. }
  689. int get_vocproc_dev_cfg_cal(struct acdb_cal_block *cal_block)
  690. {
  691. int result = 0;
  692. pr_debug("%s,\n", __func__);
  693. if (cal_block == NULL) {
  694. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  695. result = -EINVAL;
  696. goto done;
  697. }
  698. cal_block->cal_size = acdb_data.vocproc_dev_cal.cal_size;
  699. cal_block->cal_paddr = acdb_data.vocproc_dev_cal.cal_paddr;
  700. cal_block->cal_kvaddr = acdb_data.vocproc_dev_cal.cal_kvaddr;
  701. done:
  702. return result;
  703. }
  704. int store_vocproc_cal(struct cal_block *cal_block)
  705. {
  706. int result = 0;
  707. pr_debug("%s,\n", __func__);
  708. if (cal_block->cal_offset >
  709. acdb_data.mem_len) {
  710. pr_err("%s: offset %d is > mem_len %llu\n",
  711. __func__, cal_block->cal_offset, acdb_data.mem_len);
  712. acdb_data.vocproc_cal.cal_size = 0;
  713. result = -EINVAL;
  714. goto done;
  715. }
  716. acdb_data.vocproc_cal.cal_size = cal_block->cal_size;
  717. acdb_data.vocproc_cal.cal_paddr =
  718. cal_block->cal_offset + acdb_data.paddr;
  719. acdb_data.vocproc_cal.cal_kvaddr =
  720. cal_block->cal_offset + acdb_data.kvaddr;
  721. done:
  722. return result;
  723. }
  724. int get_vocproc_cal(struct acdb_cal_block *cal_block)
  725. {
  726. int result = 0;
  727. pr_debug("%s,\n", __func__);
  728. if (cal_block == NULL) {
  729. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  730. result = -EINVAL;
  731. goto done;
  732. }
  733. cal_block->cal_size = acdb_data.vocproc_cal.cal_size;
  734. cal_block->cal_paddr = acdb_data.vocproc_cal.cal_paddr;
  735. cal_block->cal_kvaddr = acdb_data.vocproc_cal.cal_kvaddr;
  736. done:
  737. return result;
  738. }
  739. int store_vocstrm_cal(struct cal_block *cal_block)
  740. {
  741. int result = 0;
  742. pr_debug("%s,\n", __func__);
  743. if (cal_block->cal_offset >
  744. acdb_data.mem_len) {
  745. pr_err("%s: offset %d is > mem_len %llu\n",
  746. __func__, cal_block->cal_offset, acdb_data.mem_len);
  747. acdb_data.vocstrm_cal.cal_size = 0;
  748. result = -EINVAL;
  749. goto done;
  750. }
  751. acdb_data.vocstrm_cal.cal_size = cal_block->cal_size;
  752. acdb_data.vocstrm_cal.cal_paddr =
  753. cal_block->cal_offset + acdb_data.paddr;
  754. acdb_data.vocstrm_cal.cal_kvaddr =
  755. cal_block->cal_offset + acdb_data.kvaddr;
  756. done:
  757. return result;
  758. }
  759. int get_vocstrm_cal(struct acdb_cal_block *cal_block)
  760. {
  761. int result = 0;
  762. pr_debug("%s,\n", __func__);
  763. if (cal_block == NULL) {
  764. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  765. result = -EINVAL;
  766. goto done;
  767. }
  768. cal_block->cal_size = acdb_data.vocstrm_cal.cal_size;
  769. cal_block->cal_paddr = acdb_data.vocstrm_cal.cal_paddr;
  770. cal_block->cal_kvaddr = acdb_data.vocstrm_cal.cal_kvaddr;
  771. done:
  772. return result;
  773. }
  774. int store_vocvol_cal(struct cal_block *cal_block)
  775. {
  776. int result = 0;
  777. pr_debug("%s,\n", __func__);
  778. if (cal_block->cal_offset > acdb_data.mem_len) {
  779. pr_err("%s: offset %d is > mem_len %llu\n",
  780. __func__, cal_block->cal_offset, acdb_data.mem_len);
  781. acdb_data.vocvol_cal.cal_size = 0;
  782. result = -EINVAL;
  783. goto done;
  784. }
  785. acdb_data.vocvol_cal.cal_size = cal_block->cal_size;
  786. acdb_data.vocvol_cal.cal_paddr =
  787. cal_block->cal_offset + acdb_data.paddr;
  788. acdb_data.vocvol_cal.cal_kvaddr =
  789. cal_block->cal_offset + acdb_data.kvaddr;
  790. done:
  791. return result;
  792. }
  793. int get_vocvol_cal(struct acdb_cal_block *cal_block)
  794. {
  795. int result = 0;
  796. pr_debug("%s,\n", __func__);
  797. if (cal_block == NULL) {
  798. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  799. result = -EINVAL;
  800. goto done;
  801. }
  802. cal_block->cal_size = acdb_data.vocvol_cal.cal_size;
  803. cal_block->cal_paddr = acdb_data.vocvol_cal.cal_paddr;
  804. cal_block->cal_kvaddr = acdb_data.vocvol_cal.cal_kvaddr;
  805. done:
  806. return result;
  807. }
  808. void store_sidetone_cal(struct sidetone_cal *cal_data)
  809. {
  810. pr_debug("%s,\n", __func__);
  811. acdb_data.sidetone_cal.enable = cal_data->enable;
  812. acdb_data.sidetone_cal.gain = cal_data->gain;
  813. }
  814. int get_sidetone_cal(struct sidetone_cal *cal_data)
  815. {
  816. int result = 0;
  817. pr_debug("%s,\n", __func__);
  818. if (cal_data == NULL) {
  819. pr_err("ACDB=> NULL pointer sent to %s\n", __func__);
  820. result = -EINVAL;
  821. goto done;
  822. }
  823. mutex_lock(&acdb_data.acdb_mutex);
  824. cal_data->enable = acdb_data.sidetone_cal.enable;
  825. cal_data->gain = acdb_data.sidetone_cal.gain;
  826. mutex_unlock(&acdb_data.acdb_mutex);
  827. done:
  828. return result;
  829. }
  830. int get_spk_protection_cfg(struct msm_spk_prot_cfg *prot_cfg)
  831. {
  832. int result = 0;
  833. pr_debug("%s,\n", __func__);
  834. mutex_lock(&acdb_data.acdb_mutex);
  835. if (prot_cfg) {
  836. prot_cfg->mode = acdb_data.spk_prot_cfg.mode;
  837. prot_cfg->r0 = acdb_data.spk_prot_cfg.r0;
  838. prot_cfg->t0 = acdb_data.spk_prot_cfg.t0;
  839. } else {
  840. pr_err("%s prot_cfg is NULL\n", __func__);
  841. result = -EINVAL;
  842. }
  843. mutex_unlock(&acdb_data.acdb_mutex);
  844. return result;
  845. }
  846. static int get_spk_protection_status(struct msm_spk_prot_status *status)
  847. {
  848. int result = 0;
  849. struct afe_spkr_prot_get_vi_calib calib_resp;
  850. pr_debug("%s,\n", __func__);
  851. /*Call AFE function here to query the status*/
  852. if (status) {
  853. status->status = -EINVAL;
  854. if (!afe_spk_prot_get_calib_data(&calib_resp)) {
  855. if (calib_resp.res_cfg.th_vi_ca_state == 1)
  856. status->status = -EAGAIN;
  857. else if (calib_resp.res_cfg.th_vi_ca_state == 2) {
  858. status->status = 0;
  859. status->r0 = calib_resp.res_cfg.r0_cali_q24;
  860. }
  861. }
  862. } else {
  863. pr_err("%s invalid params\n", __func__);
  864. result = -EINVAL;
  865. }
  866. return result;
  867. }
  868. static int register_vocvol_table(void)
  869. {
  870. int result = 0;
  871. pr_debug("%s\n", __func__);
  872. result = voc_register_vocproc_vol_table();
  873. if (result < 0) {
  874. pr_err("%s: Register vocproc vol failed!\n", __func__);
  875. goto done;
  876. }
  877. done:
  878. return result;
  879. }
  880. static int deregister_vocvol_table(void)
  881. {
  882. int result = 0;
  883. pr_debug("%s\n", __func__);
  884. result = voc_deregister_vocproc_vol_table();
  885. if (result < 0) {
  886. pr_err("%s: Deregister vocproc vol failed!\n", __func__);
  887. goto done;
  888. }
  889. done:
  890. return result;
  891. }
  892. static int acdb_open(struct inode *inode, struct file *f)
  893. {
  894. s32 result = 0;
  895. pr_debug("%s\n", __func__);
  896. mutex_lock(&acdb_data.acdb_mutex);
  897. if (acdb_data.mem_len)
  898. pr_debug("%s: ACDB opened but memory allocated, using existing allocation!\n",
  899. __func__);
  900. acdb_data.valid_adm_custom_top = 1;
  901. acdb_data.valid_asm_custom_top = 1;
  902. acdb_data.usage_count++;
  903. mutex_unlock(&acdb_data.acdb_mutex);
  904. return result;
  905. }
  906. static void deallocate_hw_delay_entries(void)
  907. {
  908. kfree(acdb_data.hw_delay_rx.delay_info);
  909. kfree(acdb_data.hw_delay_tx.delay_info);
  910. acdb_data.hw_delay_rx.delay_info = NULL;
  911. acdb_data.hw_delay_tx.delay_info = NULL;
  912. }
  913. static int allocate_hw_delay_entries(void)
  914. {
  915. int result = 0;
  916. /* Allocate memory for hw delay entries */
  917. acdb_data.hw_delay_rx.num_entries = 0;
  918. acdb_data.hw_delay_tx.num_entries = 0;
  919. acdb_data.hw_delay_rx.delay_info =
  920. kmalloc(sizeof(struct hw_delay_entry)*
  921. MAX_HW_DELAY_ENTRIES,
  922. GFP_KERNEL);
  923. if (acdb_data.hw_delay_rx.delay_info == NULL) {
  924. pr_err("%s : Failed to allocate av sync delay entries rx\n",
  925. __func__);
  926. result = -ENOMEM;
  927. goto done;
  928. }
  929. acdb_data.hw_delay_tx.delay_info =
  930. kmalloc(sizeof(struct hw_delay_entry)*
  931. MAX_HW_DELAY_ENTRIES,
  932. GFP_KERNEL);
  933. if (acdb_data.hw_delay_tx.delay_info == NULL) {
  934. pr_err("%s : Failed to allocate av sync delay entries tx\n",
  935. __func__);
  936. deallocate_hw_delay_entries();
  937. result = -ENOMEM;
  938. goto done;
  939. }
  940. done:
  941. return result;
  942. }
  943. static void deallocate_col_data(void)
  944. {
  945. int i;
  946. for (i = 0; i < MAX_VOCPROC_TYPES; i++) {
  947. kfree(acdb_data.col_data[i]);
  948. acdb_data.col_data[i] = NULL;
  949. }
  950. }
  951. static int allocate_col_data(void)
  952. {
  953. int result = 0;
  954. int i;
  955. for (i = 0; i < MAX_VOCPROC_TYPES; i++) {
  956. acdb_data.col_data[i] = kmalloc(MAX_COL_SIZE, GFP_KERNEL);
  957. if (acdb_data.col_data[i] == NULL) {
  958. pr_err("%s: kmalloc column data failed, type = %d\n",
  959. __func__, i);
  960. deallocate_col_data();
  961. result = -ENOMEM;
  962. goto done;
  963. }
  964. acdb_data.vocproc_col_cal[i].cal_kvaddr =
  965. (uint32_t)acdb_data.col_data[i];
  966. }
  967. done:
  968. return result;
  969. }
  970. static int unmap_cal_tables(void)
  971. {
  972. int result = 0;
  973. int result2 = 0;
  974. result2 = adm_unmap_cal_blocks();
  975. if (result2 < 0) {
  976. pr_err("%s: adm_unmap_cal_blocks failed, err = %d\n",
  977. __func__, result2);
  978. result = result2;
  979. }
  980. result2 = afe_unmap_cal_blocks();
  981. if (result2 < 0) {
  982. pr_err("%s: afe_unmap_cal_blocks failed, err = %d\n",
  983. __func__, result2);
  984. result = result2;
  985. }
  986. result2 = q6asm_unmap_cal_blocks();
  987. if (result2 < 0) {
  988. pr_err("%s: asm_unmap_cal_blocks failed, err = %d\n",
  989. __func__, result2);
  990. result = result2;
  991. }
  992. result2 = voc_unmap_cal_blocks();
  993. if (result2 < 0) {
  994. pr_err("%s: voice_unmap_cal_blocks failed, err = %d\n",
  995. __func__, result2);
  996. result = result2;
  997. }
  998. return result;
  999. }
  1000. static int deregister_memory(void)
  1001. {
  1002. int result = 0;
  1003. pr_debug("%s\n", __func__);
  1004. if (acdb_data.mem_len == 0)
  1005. goto done;
  1006. pr_debug("Remove existing memory\n");
  1007. acdb_data.mem_len = 0;
  1008. /* unmap all cal data */
  1009. result = unmap_cal_tables();
  1010. if (result < 0)
  1011. pr_err("%s: unmap_cal_tables failed, err = %d\n",
  1012. __func__, result);
  1013. msm_audio_ion_free(acdb_data.ion_client, acdb_data.ion_handle);
  1014. acdb_data.ion_client = NULL;
  1015. acdb_data.ion_handle = NULL;
  1016. deallocate_col_data();
  1017. deallocate_hw_delay_entries();
  1018. done:
  1019. return result;
  1020. }
  1021. static int register_memory(void)
  1022. {
  1023. int result;
  1024. ion_phys_addr_t paddr;
  1025. void *kvptr;
  1026. unsigned long kvaddr;
  1027. unsigned long mem_len;
  1028. pr_debug("%s\n", __func__);
  1029. result = allocate_col_data();
  1030. if (result) {
  1031. pr_err("%s: allocate_col_data failed, rc = %d\n",
  1032. __func__, result);
  1033. goto err_done;
  1034. }
  1035. result = allocate_hw_delay_entries();
  1036. if (result) {
  1037. pr_err("%s: allocate_hw_delay_entries failed, rc = %d\n",
  1038. __func__, result);
  1039. goto err_col;
  1040. }
  1041. result = msm_audio_ion_import("audio_acdb_client",
  1042. &acdb_data.ion_client,
  1043. &acdb_data.ion_handle,
  1044. acdb_data.map_handle,
  1045. NULL, 0,
  1046. &paddr, (size_t *)&mem_len, &kvptr);
  1047. if (result) {
  1048. pr_err("%s: audio ION alloc failed, rc = %d\n",
  1049. __func__, result);
  1050. goto err_hw_delay;
  1051. }
  1052. kvaddr = (unsigned long)kvptr;
  1053. acdb_data.paddr = paddr;
  1054. acdb_data.kvaddr = kvaddr;
  1055. acdb_data.mem_len = mem_len;
  1056. pr_debug("%s done! paddr = 0x%llx, kvaddr = 0x%llx, len = 0x%llx\n",
  1057. __func__, acdb_data.paddr, acdb_data.kvaddr,
  1058. acdb_data.mem_len);
  1059. return result;
  1060. err_hw_delay:
  1061. deallocate_hw_delay_entries();
  1062. err_col:
  1063. deallocate_col_data();
  1064. err_done:
  1065. acdb_data.mem_len = 0;
  1066. return result;
  1067. }
  1068. static long acdb_ioctl(struct file *f,
  1069. unsigned int cmd, unsigned long arg)
  1070. {
  1071. int32_t result = 0;
  1072. int32_t size;
  1073. int32_t map_fd;
  1074. uint32_t topology;
  1075. uint32_t data[MAX_IOCTL_DATA];
  1076. struct msm_spk_prot_status prot_status;
  1077. struct msm_spk_prot_status acdb_spk_status;
  1078. pr_debug("%s\n", __func__);
  1079. mutex_lock(&acdb_data.acdb_mutex);
  1080. switch (cmd) {
  1081. case AUDIO_REGISTER_PMEM:
  1082. pr_debug("AUDIO_REGISTER_PMEM\n");
  1083. result = deregister_memory();
  1084. if (result < 0)
  1085. pr_err("%s: deregister_memory failed returned %d!\n",
  1086. __func__, result);
  1087. if (copy_from_user(&map_fd, (void *)arg, sizeof(map_fd))) {
  1088. pr_err("%s: fail to copy memory handle!\n", __func__);
  1089. result = -EFAULT;
  1090. } else {
  1091. acdb_data.map_handle = map_fd;
  1092. result = register_memory();
  1093. }
  1094. goto done;
  1095. case AUDIO_DEREGISTER_PMEM:
  1096. pr_debug("AUDIO_DEREGISTER_PMEM\n");
  1097. result = deregister_memory();
  1098. goto done;
  1099. case AUDIO_SET_VOICE_RX_TOPOLOGY:
  1100. if (copy_from_user(&topology, (void *)arg,
  1101. sizeof(topology))) {
  1102. pr_err("%s: fail to copy topology!\n", __func__);
  1103. result = -EFAULT;
  1104. }
  1105. store_voice_rx_topology(topology);
  1106. goto done;
  1107. case AUDIO_SET_VOICE_TX_TOPOLOGY:
  1108. if (copy_from_user(&topology, (void *)arg,
  1109. sizeof(topology))) {
  1110. pr_err("%s: fail to copy topology!\n", __func__);
  1111. result = -EFAULT;
  1112. }
  1113. store_voice_tx_topology(topology);
  1114. goto done;
  1115. case AUDIO_SET_ADM_RX_TOPOLOGY:
  1116. if (copy_from_user(&topology, (void *)arg,
  1117. sizeof(topology))) {
  1118. pr_err("%s: fail to copy topology!\n", __func__);
  1119. result = -EFAULT;
  1120. }
  1121. store_adm_rx_topology(topology);
  1122. goto done;
  1123. case AUDIO_SET_ADM_TX_TOPOLOGY:
  1124. if (copy_from_user(&topology, (void *)arg,
  1125. sizeof(topology))) {
  1126. pr_err("%s: fail to copy topology!\n", __func__);
  1127. result = -EFAULT;
  1128. }
  1129. store_adm_tx_topology(topology);
  1130. goto done;
  1131. case AUDIO_SET_ASM_TOPOLOGY:
  1132. if (copy_from_user(&topology, (void *)arg,
  1133. sizeof(topology))) {
  1134. pr_err("%s: fail to copy topology!\n", __func__);
  1135. result = -EFAULT;
  1136. }
  1137. store_asm_topology(topology);
  1138. goto done;
  1139. case AUDIO_SET_SPEAKER_PROT:
  1140. if (copy_from_user(&acdb_data.spk_prot_cfg, (void *)arg,
  1141. sizeof(acdb_data.spk_prot_cfg))) {
  1142. pr_err("%s fail to copy spk_prot_cfg\n", __func__);
  1143. result = -EFAULT;
  1144. }
  1145. goto done;
  1146. case AUDIO_GET_SPEAKER_PROT:
  1147. /*Indicates calibration was succesfull*/
  1148. if (acdb_data.spk_prot_cfg.mode == MSM_SPKR_PROT_CALIBRATED) {
  1149. prot_status.r0 = acdb_data.spk_prot_cfg.r0;
  1150. prot_status.status = 0;
  1151. } else if (acdb_data.spk_prot_cfg.mode ==
  1152. MSM_SPKR_PROT_CALIBRATION_IN_PROGRESS) {
  1153. /*Call AFE to query the status*/
  1154. acdb_spk_status.status = -EINVAL;
  1155. acdb_spk_status.r0 = -1;
  1156. get_spk_protection_status(&acdb_spk_status);
  1157. prot_status.r0 = acdb_spk_status.r0;
  1158. prot_status.status = acdb_spk_status.status;
  1159. if (!acdb_spk_status.status) {
  1160. acdb_data.spk_prot_cfg.mode =
  1161. MSM_SPKR_PROT_CALIBRATED;
  1162. acdb_data.spk_prot_cfg.r0 = prot_status.r0;
  1163. }
  1164. } else {
  1165. /*Indicates calibration data is invalid*/
  1166. prot_status.status = -EINVAL;
  1167. prot_status.r0 = -1;
  1168. }
  1169. if (copy_to_user((void *)arg, &prot_status,
  1170. sizeof(prot_status))) {
  1171. pr_err("%s: Failed to update prot_status\n", __func__);
  1172. }
  1173. goto done;
  1174. case AUDIO_REGISTER_VOCPROC_VOL_TABLE:
  1175. result = register_vocvol_table();
  1176. goto done;
  1177. case AUDIO_DEREGISTER_VOCPROC_VOL_TABLE:
  1178. result = deregister_vocvol_table();
  1179. goto done;
  1180. case AUDIO_SET_HW_DELAY_RX:
  1181. result = store_hw_delay(RX_CAL, (void *)arg);
  1182. goto done;
  1183. case AUDIO_SET_HW_DELAY_TX:
  1184. result = store_hw_delay(TX_CAL, (void *)arg);
  1185. goto done;
  1186. }
  1187. if (copy_from_user(&size, (void *) arg, sizeof(size))) {
  1188. result = -EFAULT;
  1189. goto done;
  1190. }
  1191. if ((size <= 0) || (size > sizeof(data))) {
  1192. pr_err("%s: Invalid size sent to driver: %d\n",
  1193. __func__, size);
  1194. result = -EFAULT;
  1195. goto done;
  1196. }
  1197. switch (cmd) {
  1198. case AUDIO_SET_VOCPROC_COL_CAL:
  1199. result = store_voice_col_data(VOCPROC_CAL,
  1200. size, (uint32_t *)arg);
  1201. goto done;
  1202. case AUDIO_SET_VOCSTRM_COL_CAL:
  1203. result = store_voice_col_data(VOCSTRM_CAL,
  1204. size, (uint32_t *)arg);
  1205. goto done;
  1206. case AUDIO_SET_VOCVOL_COL_CAL:
  1207. result = store_voice_col_data(VOCVOL_CAL,
  1208. size, (uint32_t *)arg);
  1209. goto done;
  1210. }
  1211. if (copy_from_user(data, (void *)(arg + sizeof(size)), size)) {
  1212. pr_err("%s: fail to copy table size %d\n", __func__, size);
  1213. result = -EFAULT;
  1214. goto done;
  1215. }
  1216. if (data == NULL) {
  1217. pr_err("%s: NULL pointer sent to driver!\n", __func__);
  1218. result = -EFAULT;
  1219. goto done;
  1220. }
  1221. if (size > sizeof(struct cal_block))
  1222. pr_err("%s: More cal data for ioctl 0x%x then expected, size received: %d\n",
  1223. __func__, cmd, size);
  1224. switch (cmd) {
  1225. case AUDIO_SET_AUDPROC_TX_CAL:
  1226. result = store_audproc_cal(TX_CAL, (struct cal_block *)data);
  1227. goto done;
  1228. case AUDIO_SET_AUDPROC_RX_CAL:
  1229. result = store_audproc_cal(RX_CAL, (struct cal_block *)data);
  1230. goto done;
  1231. case AUDIO_SET_AUDPROC_TX_STREAM_CAL:
  1232. result = store_audstrm_cal(TX_CAL, (struct cal_block *)data);
  1233. goto done;
  1234. case AUDIO_SET_AUDPROC_RX_STREAM_CAL:
  1235. result = store_audstrm_cal(RX_CAL, (struct cal_block *)data);
  1236. goto done;
  1237. case AUDIO_SET_AUDPROC_TX_VOL_CAL:
  1238. result = store_audvol_cal(TX_CAL, (struct cal_block *)data);
  1239. goto done;
  1240. case AUDIO_SET_AUDPROC_RX_VOL_CAL:
  1241. result = store_audvol_cal(RX_CAL, (struct cal_block *)data);
  1242. goto done;
  1243. case AUDIO_SET_AFE_TX_CAL:
  1244. result = store_afe_cal(TX_CAL, (struct cal_block *)data);
  1245. goto done;
  1246. case AUDIO_SET_AFE_RX_CAL:
  1247. result = store_afe_cal(RX_CAL, (struct cal_block *)data);
  1248. goto done;
  1249. case AUDIO_SET_VOCPROC_CAL:
  1250. result = store_vocproc_cal((struct cal_block *)data);
  1251. goto done;
  1252. case AUDIO_SET_VOCPROC_STREAM_CAL:
  1253. result = store_vocstrm_cal((struct cal_block *)data);
  1254. goto done;
  1255. case AUDIO_SET_VOCPROC_VOL_CAL:
  1256. result = store_vocvol_cal((struct cal_block *)data);
  1257. goto done;
  1258. case AUDIO_SET_VOCPROC_DEV_CFG_CAL:
  1259. result = store_vocproc_dev_cfg_cal((struct cal_block *)data);
  1260. goto done;
  1261. case AUDIO_SET_SIDETONE_CAL:
  1262. store_sidetone_cal((struct sidetone_cal *)data);
  1263. goto done;
  1264. case AUDIO_SET_ANC_CAL:
  1265. result = store_anc_cal((struct cal_block *)data);
  1266. goto done;
  1267. case AUDIO_SET_LSM_CAL:
  1268. result = store_lsm_cal((struct cal_block *)data);
  1269. goto done;
  1270. case AUDIO_SET_ADM_CUSTOM_TOPOLOGY:
  1271. result = store_adm_custom_topology((struct cal_block *)data);
  1272. goto done;
  1273. case AUDIO_SET_ASM_CUSTOM_TOPOLOGY:
  1274. result = store_asm_custom_topology((struct cal_block *)data);
  1275. goto done;
  1276. case AUDIO_SET_AANC_CAL:
  1277. result = store_aanc_cal((struct cal_block *)data);
  1278. goto done;
  1279. default:
  1280. pr_err("ACDB=> ACDB ioctl not found!\n");
  1281. result = -EFAULT;
  1282. goto done;
  1283. }
  1284. done:
  1285. mutex_unlock(&acdb_data.acdb_mutex);
  1286. return result;
  1287. }
  1288. static int acdb_mmap(struct file *file, struct vm_area_struct *vma)
  1289. {
  1290. int result = 0;
  1291. size_t size = vma->vm_end - vma->vm_start;
  1292. pr_debug("%s\n", __func__);
  1293. mutex_lock(&acdb_data.acdb_mutex);
  1294. if (acdb_data.mem_len) {
  1295. if (size <= acdb_data.mem_len) {
  1296. vma->vm_page_prot = pgprot_noncached(
  1297. vma->vm_page_prot);
  1298. result = remap_pfn_range(vma,
  1299. vma->vm_start,
  1300. acdb_data.paddr >> PAGE_SHIFT,
  1301. size,
  1302. vma->vm_page_prot);
  1303. } else {
  1304. pr_err("%s: Not enough memory!\n", __func__);
  1305. result = -ENOMEM;
  1306. }
  1307. } else {
  1308. pr_err("%s: memory is not allocated, yet!\n", __func__);
  1309. result = -ENODEV;
  1310. }
  1311. mutex_unlock(&acdb_data.acdb_mutex);
  1312. return result;
  1313. }
  1314. static int acdb_release(struct inode *inode, struct file *f)
  1315. {
  1316. int result = 0;
  1317. pr_debug("%s\n", __func__);
  1318. mutex_lock(&acdb_data.acdb_mutex);
  1319. acdb_data.usage_count--;
  1320. pr_debug("%s: ref count %d!\n", __func__, acdb_data.usage_count);
  1321. if (acdb_data.usage_count > 0) {
  1322. result = -EBUSY;
  1323. goto done;
  1324. }
  1325. result = deregister_memory();
  1326. done:
  1327. mutex_unlock(&acdb_data.acdb_mutex);
  1328. return result;
  1329. }
  1330. static const struct file_operations acdb_fops = {
  1331. .owner = THIS_MODULE,
  1332. .open = acdb_open,
  1333. .release = acdb_release,
  1334. .unlocked_ioctl = acdb_ioctl,
  1335. .mmap = acdb_mmap,
  1336. };
  1337. struct miscdevice acdb_misc = {
  1338. .minor = MISC_DYNAMIC_MINOR,
  1339. .name = "msm_acdb",
  1340. .fops = &acdb_fops,
  1341. };
  1342. static int __init acdb_init(void)
  1343. {
  1344. memset(&acdb_data, 0, sizeof(acdb_data));
  1345. /*Speaker protection disabled*/
  1346. acdb_data.spk_prot_cfg.mode = MSM_SPKR_PROT_DISABLED;
  1347. mutex_init(&acdb_data.acdb_mutex);
  1348. acdb_data.usage_count = 0;
  1349. acdb_data.valid_adm_custom_top = 1;
  1350. acdb_data.valid_asm_custom_top = 1;
  1351. return misc_register(&acdb_misc);
  1352. }
  1353. static void __exit acdb_exit(void)
  1354. {
  1355. }
  1356. module_init(acdb_init);
  1357. module_exit(acdb_exit);
  1358. MODULE_DESCRIPTION("SoC QDSP6v2 Audio ACDB driver");
  1359. MODULE_LICENSE("GPL v2");