cygnus-ssp.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530
  1. /*
  2. * Copyright (C) 2014-2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/slab.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/soc-dai.h>
  25. #include "cygnus-ssp.h"
  26. #define DEFAULT_VCO 1354750204
  27. #define CYGNUS_TDM_RATE \
  28. (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
  29. SNDRV_PCM_RATE_11025 | SNDRV_PCM_RATE_22050 | \
  30. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
  31. SNDRV_PCM_RATE_48000)
  32. #define CAPTURE_FCI_ID_BASE 0x180
  33. #define CYGNUS_SSP_TRISTATE_MASK 0x001fff
  34. #define CYGNUS_PLLCLKSEL_MASK 0xf
  35. /* Used with stream_on field to indicate which streams are active */
  36. #define PLAYBACK_STREAM_MASK BIT(0)
  37. #define CAPTURE_STREAM_MASK BIT(1)
  38. #define I2S_STREAM_CFG_MASK 0xff003ff
  39. #define I2S_CAP_STREAM_CFG_MASK 0xf0
  40. #define SPDIF_STREAM_CFG_MASK 0x3ff
  41. #define CH_GRP_STEREO 0x1
  42. /* Begin register offset defines */
  43. #define AUD_MISC_SEROUT_OE_REG_BASE 0x01c
  44. #define AUD_MISC_SEROUT_SPDIF_OE 12
  45. #define AUD_MISC_SEROUT_MCLK_OE 3
  46. #define AUD_MISC_SEROUT_LRCK_OE 2
  47. #define AUD_MISC_SEROUT_SCLK_OE 1
  48. #define AUD_MISC_SEROUT_SDAT_OE 0
  49. /* AUD_FMM_BF_CTRL_xxx regs */
  50. #define BF_DST_CFG0_OFFSET 0x100
  51. #define BF_DST_CFG1_OFFSET 0x104
  52. #define BF_DST_CFG2_OFFSET 0x108
  53. #define BF_DST_CTRL0_OFFSET 0x130
  54. #define BF_DST_CTRL1_OFFSET 0x134
  55. #define BF_DST_CTRL2_OFFSET 0x138
  56. #define BF_SRC_CFG0_OFFSET 0x148
  57. #define BF_SRC_CFG1_OFFSET 0x14c
  58. #define BF_SRC_CFG2_OFFSET 0x150
  59. #define BF_SRC_CFG3_OFFSET 0x154
  60. #define BF_SRC_CTRL0_OFFSET 0x1c0
  61. #define BF_SRC_CTRL1_OFFSET 0x1c4
  62. #define BF_SRC_CTRL2_OFFSET 0x1c8
  63. #define BF_SRC_CTRL3_OFFSET 0x1cc
  64. #define BF_SRC_GRP0_OFFSET 0x1fc
  65. #define BF_SRC_GRP1_OFFSET 0x200
  66. #define BF_SRC_GRP2_OFFSET 0x204
  67. #define BF_SRC_GRP3_OFFSET 0x208
  68. #define BF_SRC_GRP_EN_OFFSET 0x320
  69. #define BF_SRC_GRP_FLOWON_OFFSET 0x324
  70. #define BF_SRC_GRP_SYNC_DIS_OFFSET 0x328
  71. /* AUD_FMM_IOP_OUT_I2S_xxx regs */
  72. #define OUT_I2S_0_STREAM_CFG_OFFSET 0xa00
  73. #define OUT_I2S_0_CFG_OFFSET 0xa04
  74. #define OUT_I2S_0_MCLK_CFG_OFFSET 0xa0c
  75. #define OUT_I2S_1_STREAM_CFG_OFFSET 0xa40
  76. #define OUT_I2S_1_CFG_OFFSET 0xa44
  77. #define OUT_I2S_1_MCLK_CFG_OFFSET 0xa4c
  78. #define OUT_I2S_2_STREAM_CFG_OFFSET 0xa80
  79. #define OUT_I2S_2_CFG_OFFSET 0xa84
  80. #define OUT_I2S_2_MCLK_CFG_OFFSET 0xa8c
  81. /* AUD_FMM_IOP_OUT_SPDIF_xxx regs */
  82. #define SPDIF_STREAM_CFG_OFFSET 0xac0
  83. #define SPDIF_CTRL_OFFSET 0xac4
  84. #define SPDIF_FORMAT_CFG_OFFSET 0xad8
  85. #define SPDIF_MCLK_CFG_OFFSET 0xadc
  86. /* AUD_FMM_IOP_PLL_0_xxx regs */
  87. #define IOP_PLL_0_MACRO_OFFSET 0xb00
  88. #define IOP_PLL_0_MDIV_Ch0_OFFSET 0xb14
  89. #define IOP_PLL_0_MDIV_Ch1_OFFSET 0xb18
  90. #define IOP_PLL_0_MDIV_Ch2_OFFSET 0xb1c
  91. #define IOP_PLL_0_ACTIVE_MDIV_Ch0_OFFSET 0xb30
  92. #define IOP_PLL_0_ACTIVE_MDIV_Ch1_OFFSET 0xb34
  93. #define IOP_PLL_0_ACTIVE_MDIV_Ch2_OFFSET 0xb38
  94. /* AUD_FMM_IOP_xxx regs */
  95. #define IOP_PLL_0_CONTROL_OFFSET 0xb04
  96. #define IOP_PLL_0_USER_NDIV_OFFSET 0xb08
  97. #define IOP_PLL_0_ACTIVE_NDIV_OFFSET 0xb20
  98. #define IOP_PLL_0_RESET_OFFSET 0xb5c
  99. /* AUD_FMM_IOP_IN_I2S_xxx regs */
  100. #define IN_I2S_0_STREAM_CFG_OFFSET 0x00
  101. #define IN_I2S_0_CFG_OFFSET 0x04
  102. #define IN_I2S_1_STREAM_CFG_OFFSET 0x40
  103. #define IN_I2S_1_CFG_OFFSET 0x44
  104. #define IN_I2S_2_STREAM_CFG_OFFSET 0x80
  105. #define IN_I2S_2_CFG_OFFSET 0x84
  106. /* AUD_FMM_IOP_MISC_xxx regs */
  107. #define IOP_SW_INIT_LOGIC 0x1c0
  108. /* End register offset defines */
  109. /* AUD_FMM_IOP_OUT_I2S_x_MCLK_CFG_0_REG */
  110. #define I2S_OUT_MCLKRATE_SHIFT 16
  111. /* AUD_FMM_IOP_OUT_I2S_x_MCLK_CFG_REG */
  112. #define I2S_OUT_PLLCLKSEL_SHIFT 0
  113. /* AUD_FMM_IOP_OUT_I2S_x_STREAM_CFG */
  114. #define I2S_OUT_STREAM_ENA 31
  115. #define I2S_OUT_STREAM_CFG_GROUP_ID 20
  116. #define I2S_OUT_STREAM_CFG_CHANNEL_GROUPING 24
  117. /* AUD_FMM_IOP_IN_I2S_x_CAP */
  118. #define I2S_IN_STREAM_CFG_CAP_ENA 31
  119. #define I2S_IN_STREAM_CFG_0_GROUP_ID 4
  120. /* AUD_FMM_IOP_OUT_I2S_x_I2S_CFG_REG */
  121. #define I2S_OUT_CFGX_CLK_ENA 0
  122. #define I2S_OUT_CFGX_DATA_ENABLE 1
  123. #define I2S_OUT_CFGX_DATA_ALIGNMENT 6
  124. #define I2S_OUT_CFGX_BITS_PER_SLOT 13
  125. #define I2S_OUT_CFGX_VALID_SLOT 14
  126. #define I2S_OUT_CFGX_FSYNC_WIDTH 18
  127. #define I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32 26
  128. #define I2S_OUT_CFGX_SLAVE_MODE 30
  129. #define I2S_OUT_CFGX_TDM_MODE 31
  130. /* AUD_FMM_BF_CTRL_SOURCECH_CFGx_REG */
  131. #define BF_SRC_CFGX_SFIFO_ENA 0
  132. #define BF_SRC_CFGX_BUFFER_PAIR_ENABLE 1
  133. #define BF_SRC_CFGX_SAMPLE_CH_MODE 2
  134. #define BF_SRC_CFGX_SFIFO_SZ_DOUBLE 5
  135. #define BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY 10
  136. #define BF_SRC_CFGX_BIT_RES 20
  137. #define BF_SRC_CFGX_PROCESS_SEQ_ID_VALID 31
  138. /* AUD_FMM_BF_CTRL_DESTCH_CFGx_REG */
  139. #define BF_DST_CFGX_CAP_ENA 0
  140. #define BF_DST_CFGX_BUFFER_PAIR_ENABLE 1
  141. #define BF_DST_CFGX_DFIFO_SZ_DOUBLE 2
  142. #define BF_DST_CFGX_NOT_PAUSE_WHEN_FULL 11
  143. #define BF_DST_CFGX_FCI_ID 12
  144. #define BF_DST_CFGX_CAP_MODE 24
  145. #define BF_DST_CFGX_PROC_SEQ_ID_VALID 31
  146. /* AUD_FMM_IOP_OUT_SPDIF_xxx */
  147. #define SPDIF_0_OUT_DITHER_ENA 3
  148. #define SPDIF_0_OUT_STREAM_ENA 31
  149. /* AUD_FMM_IOP_PLL_0_USER */
  150. #define IOP_PLL_0_USER_NDIV_FRAC 10
  151. /* AUD_FMM_IOP_PLL_0_ACTIVE */
  152. #define IOP_PLL_0_ACTIVE_NDIV_FRAC 10
  153. #define INIT_SSP_REGS(num) (struct cygnus_ssp_regs){ \
  154. .i2s_stream_cfg = OUT_I2S_ ##num## _STREAM_CFG_OFFSET, \
  155. .i2s_cap_stream_cfg = IN_I2S_ ##num## _STREAM_CFG_OFFSET, \
  156. .i2s_cfg = OUT_I2S_ ##num## _CFG_OFFSET, \
  157. .i2s_cap_cfg = IN_I2S_ ##num## _CFG_OFFSET, \
  158. .i2s_mclk_cfg = OUT_I2S_ ##num## _MCLK_CFG_OFFSET, \
  159. .bf_destch_ctrl = BF_DST_CTRL ##num## _OFFSET, \
  160. .bf_destch_cfg = BF_DST_CFG ##num## _OFFSET, \
  161. .bf_sourcech_ctrl = BF_SRC_CTRL ##num## _OFFSET, \
  162. .bf_sourcech_cfg = BF_SRC_CFG ##num## _OFFSET, \
  163. .bf_sourcech_grp = BF_SRC_GRP ##num## _OFFSET \
  164. }
  165. struct pll_macro_entry {
  166. u32 mclk;
  167. u32 pll_ch_num;
  168. };
  169. /*
  170. * PLL has 3 output channels (1x, 2x, and 4x). Below are
  171. * the common MCLK frequencies used by audio driver
  172. */
  173. static const struct pll_macro_entry pll_predef_mclk[] = {
  174. { 4096000, 0},
  175. { 8192000, 1},
  176. {16384000, 2},
  177. { 5644800, 0},
  178. {11289600, 1},
  179. {22579200, 2},
  180. { 6144000, 0},
  181. {12288000, 1},
  182. {24576000, 2},
  183. {12288000, 0},
  184. {24576000, 1},
  185. {49152000, 2},
  186. {22579200, 0},
  187. {45158400, 1},
  188. {90316800, 2},
  189. {24576000, 0},
  190. {49152000, 1},
  191. {98304000, 2},
  192. };
  193. /* List of valid frame sizes for tdm mode */
  194. static const int ssp_valid_tdm_framesize[] = {32, 64, 128, 256, 512};
  195. /*
  196. * Use this relationship to derive the sampling rate (lrclk)
  197. * lrclk = (mclk) / ((2*mclk_to_sclk_ratio) * (32 * SCLK))).
  198. *
  199. * Use mclk and pll_ch from the table above
  200. *
  201. * Valid SCLK = 0/1/2/4/8/12
  202. *
  203. * mclk_to_sclk_ratio = number of MCLK per SCLK. Division is twice the
  204. * value programmed in this field.
  205. * Valid mclk_to_sclk_ratio = 1 through to 15
  206. *
  207. * eg: To set lrclk = 48khz, set mclk = 12288000, mclk_to_sclk_ratio = 2,
  208. * SCLK = 64
  209. */
  210. struct _ssp_clk_coeff {
  211. u32 mclk;
  212. u32 sclk_rate;
  213. u32 rate;
  214. u32 mclk_rate;
  215. };
  216. static const struct _ssp_clk_coeff ssp_clk_coeff[] = {
  217. { 4096000, 32, 16000, 4},
  218. { 4096000, 32, 32000, 2},
  219. { 4096000, 64, 8000, 4},
  220. { 4096000, 64, 16000, 2},
  221. { 4096000, 64, 32000, 1},
  222. { 4096000, 128, 8000, 2},
  223. { 4096000, 128, 16000, 1},
  224. { 4096000, 256, 8000, 1},
  225. { 6144000, 32, 16000, 6},
  226. { 6144000, 32, 32000, 3},
  227. { 6144000, 32, 48000, 2},
  228. { 6144000, 32, 96000, 1},
  229. { 6144000, 64, 8000, 6},
  230. { 6144000, 64, 16000, 3},
  231. { 6144000, 64, 48000, 1},
  232. { 6144000, 128, 8000, 3},
  233. { 8192000, 32, 32000, 4},
  234. { 8192000, 64, 16000, 4},
  235. { 8192000, 64, 32000, 2},
  236. { 8192000, 128, 8000, 4},
  237. { 8192000, 128, 16000, 2},
  238. { 8192000, 128, 32000, 1},
  239. { 8192000, 256, 8000, 2},
  240. { 8192000, 256, 16000, 1},
  241. { 8192000, 512, 8000, 1},
  242. {12288000, 32, 32000, 6},
  243. {12288000, 32, 48000, 4},
  244. {12288000, 32, 96000, 2},
  245. {12288000, 32, 192000, 1},
  246. {12288000, 64, 16000, 6},
  247. {12288000, 64, 32000, 3},
  248. {12288000, 64, 48000, 2},
  249. {12288000, 64, 96000, 1},
  250. {12288000, 128, 8000, 6},
  251. {12288000, 128, 16000, 3},
  252. {12288000, 128, 48000, 1},
  253. {12288000, 256, 8000, 3},
  254. {16384000, 64, 32000, 4},
  255. {16384000, 128, 16000, 4},
  256. {16384000, 128, 32000, 2},
  257. {16384000, 256, 8000, 4},
  258. {16384000, 256, 16000, 2},
  259. {16384000, 256, 32000, 1},
  260. {16384000, 512, 8000, 2},
  261. {16384000, 512, 16000, 1},
  262. {24576000, 32, 96000, 4},
  263. {24576000, 32, 192000, 2},
  264. {24576000, 64, 32000, 6},
  265. {24576000, 64, 48000, 4},
  266. {24576000, 64, 96000, 2},
  267. {24576000, 64, 192000, 1},
  268. {24576000, 128, 16000, 6},
  269. {24576000, 128, 32000, 3},
  270. {24576000, 128, 48000, 2},
  271. {24576000, 256, 8000, 6},
  272. {24576000, 256, 16000, 3},
  273. {24576000, 256, 48000, 1},
  274. {24576000, 512, 8000, 3},
  275. {49152000, 32, 192000, 4},
  276. {49152000, 64, 96000, 4},
  277. {49152000, 64, 192000, 2},
  278. {49152000, 128, 32000, 6},
  279. {49152000, 128, 48000, 4},
  280. {49152000, 128, 96000, 2},
  281. {49152000, 128, 192000, 1},
  282. {49152000, 256, 16000, 6},
  283. {49152000, 256, 32000, 3},
  284. {49152000, 256, 48000, 2},
  285. {49152000, 256, 96000, 1},
  286. {49152000, 512, 8000, 6},
  287. {49152000, 512, 16000, 3},
  288. {49152000, 512, 48000, 1},
  289. { 5644800, 32, 22050, 4},
  290. { 5644800, 32, 44100, 2},
  291. { 5644800, 32, 88200, 1},
  292. { 5644800, 64, 11025, 4},
  293. { 5644800, 64, 22050, 2},
  294. { 5644800, 64, 44100, 1},
  295. {11289600, 32, 44100, 4},
  296. {11289600, 32, 88200, 2},
  297. {11289600, 32, 176400, 1},
  298. {11289600, 64, 22050, 4},
  299. {11289600, 64, 44100, 2},
  300. {11289600, 64, 88200, 1},
  301. {11289600, 128, 11025, 4},
  302. {11289600, 128, 22050, 2},
  303. {11289600, 128, 44100, 1},
  304. {22579200, 32, 88200, 4},
  305. {22579200, 32, 176400, 2},
  306. {22579200, 64, 44100, 4},
  307. {22579200, 64, 88200, 2},
  308. {22579200, 64, 176400, 1},
  309. {22579200, 128, 22050, 4},
  310. {22579200, 128, 44100, 2},
  311. {22579200, 128, 88200, 1},
  312. {22579200, 256, 11025, 4},
  313. {22579200, 256, 22050, 2},
  314. {22579200, 256, 44100, 1},
  315. {45158400, 32, 176400, 4},
  316. {45158400, 64, 88200, 4},
  317. {45158400, 64, 176400, 2},
  318. {45158400, 128, 44100, 4},
  319. {45158400, 128, 88200, 2},
  320. {45158400, 128, 176400, 1},
  321. {45158400, 256, 22050, 4},
  322. {45158400, 256, 44100, 2},
  323. {45158400, 256, 88200, 1},
  324. {45158400, 512, 11025, 4},
  325. {45158400, 512, 22050, 2},
  326. {45158400, 512, 44100, 1},
  327. };
  328. static struct cygnus_aio_port *cygnus_dai_get_portinfo(struct snd_soc_dai *dai)
  329. {
  330. struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
  331. return &cygaud->portinfo[dai->id];
  332. }
  333. static int audio_ssp_init_portregs(struct cygnus_aio_port *aio)
  334. {
  335. u32 value, fci_id;
  336. int status = 0;
  337. switch (aio->port_type) {
  338. case PORT_TDM:
  339. value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
  340. value &= ~I2S_STREAM_CFG_MASK;
  341. /* Set Group ID */
  342. writel(aio->portnum,
  343. aio->cygaud->audio + aio->regs.bf_sourcech_grp);
  344. /* Configure the AUD_FMM_IOP_OUT_I2S_x_STREAM_CFG reg */
  345. value |= aio->portnum << I2S_OUT_STREAM_CFG_GROUP_ID;
  346. value |= aio->portnum; /* FCI ID is the port num */
  347. value |= CH_GRP_STEREO << I2S_OUT_STREAM_CFG_CHANNEL_GROUPING;
  348. writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
  349. /* Configure the AUD_FMM_BF_CTRL_SOURCECH_CFGX reg */
  350. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  351. value &= ~BIT(BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY);
  352. value |= BIT(BF_SRC_CFGX_SFIFO_SZ_DOUBLE);
  353. value |= BIT(BF_SRC_CFGX_PROCESS_SEQ_ID_VALID);
  354. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  355. /* Configure the AUD_FMM_IOP_IN_I2S_x_CAP_STREAM_CFG_0 reg */
  356. value = readl(aio->cygaud->i2s_in +
  357. aio->regs.i2s_cap_stream_cfg);
  358. value &= ~I2S_CAP_STREAM_CFG_MASK;
  359. value |= aio->portnum << I2S_IN_STREAM_CFG_0_GROUP_ID;
  360. writel(value, aio->cygaud->i2s_in +
  361. aio->regs.i2s_cap_stream_cfg);
  362. /* Configure the AUD_FMM_BF_CTRL_DESTCH_CFGX_REG_BASE reg */
  363. fci_id = CAPTURE_FCI_ID_BASE + aio->portnum;
  364. value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
  365. value |= BIT(BF_DST_CFGX_DFIFO_SZ_DOUBLE);
  366. value &= ~BIT(BF_DST_CFGX_NOT_PAUSE_WHEN_FULL);
  367. value |= (fci_id << BF_DST_CFGX_FCI_ID);
  368. value |= BIT(BF_DST_CFGX_PROC_SEQ_ID_VALID);
  369. writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
  370. /* Enable the transmit pin for this port */
  371. value = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  372. value &= ~BIT((aio->portnum * 4) + AUD_MISC_SEROUT_SDAT_OE);
  373. writel(value, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  374. break;
  375. case PORT_SPDIF:
  376. writel(aio->portnum, aio->cygaud->audio + BF_SRC_GRP3_OFFSET);
  377. value = readl(aio->cygaud->audio + SPDIF_CTRL_OFFSET);
  378. value |= BIT(SPDIF_0_OUT_DITHER_ENA);
  379. writel(value, aio->cygaud->audio + SPDIF_CTRL_OFFSET);
  380. /* Enable and set the FCI ID for the SPDIF channel */
  381. value = readl(aio->cygaud->audio + SPDIF_STREAM_CFG_OFFSET);
  382. value &= ~SPDIF_STREAM_CFG_MASK;
  383. value |= aio->portnum; /* FCI ID is the port num */
  384. value |= BIT(SPDIF_0_OUT_STREAM_ENA);
  385. writel(value, aio->cygaud->audio + SPDIF_STREAM_CFG_OFFSET);
  386. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  387. value &= ~BIT(BF_SRC_CFGX_NOT_PAUSE_WHEN_EMPTY);
  388. value |= BIT(BF_SRC_CFGX_SFIFO_SZ_DOUBLE);
  389. value |= BIT(BF_SRC_CFGX_PROCESS_SEQ_ID_VALID);
  390. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  391. /* Enable the spdif output pin */
  392. value = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  393. value &= ~BIT(AUD_MISC_SEROUT_SPDIF_OE);
  394. writel(value, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  395. break;
  396. default:
  397. dev_err(aio->cygaud->dev, "Port not supported\n");
  398. status = -EINVAL;
  399. }
  400. return status;
  401. }
  402. static void audio_ssp_in_enable(struct cygnus_aio_port *aio)
  403. {
  404. u32 value;
  405. value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
  406. value |= BIT(BF_DST_CFGX_CAP_ENA);
  407. writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
  408. writel(0x1, aio->cygaud->audio + aio->regs.bf_destch_ctrl);
  409. value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  410. value |= BIT(I2S_OUT_CFGX_CLK_ENA);
  411. value |= BIT(I2S_OUT_CFGX_DATA_ENABLE);
  412. writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
  413. value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
  414. value |= BIT(I2S_IN_STREAM_CFG_CAP_ENA);
  415. writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
  416. aio->streams_on |= CAPTURE_STREAM_MASK;
  417. }
  418. static void audio_ssp_in_disable(struct cygnus_aio_port *aio)
  419. {
  420. u32 value;
  421. value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
  422. value &= ~BIT(I2S_IN_STREAM_CFG_CAP_ENA);
  423. writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_stream_cfg);
  424. aio->streams_on &= ~CAPTURE_STREAM_MASK;
  425. /* If both playback and capture are off */
  426. if (!aio->streams_on) {
  427. value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  428. value &= ~BIT(I2S_OUT_CFGX_CLK_ENA);
  429. value &= ~BIT(I2S_OUT_CFGX_DATA_ENABLE);
  430. writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
  431. }
  432. writel(0x0, aio->cygaud->audio + aio->regs.bf_destch_ctrl);
  433. value = readl(aio->cygaud->audio + aio->regs.bf_destch_cfg);
  434. value &= ~BIT(BF_DST_CFGX_CAP_ENA);
  435. writel(value, aio->cygaud->audio + aio->regs.bf_destch_cfg);
  436. }
  437. static int audio_ssp_out_enable(struct cygnus_aio_port *aio)
  438. {
  439. u32 value;
  440. int status = 0;
  441. switch (aio->port_type) {
  442. case PORT_TDM:
  443. value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
  444. value |= BIT(I2S_OUT_STREAM_ENA);
  445. writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
  446. writel(1, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
  447. value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  448. value |= BIT(I2S_OUT_CFGX_CLK_ENA);
  449. value |= BIT(I2S_OUT_CFGX_DATA_ENABLE);
  450. writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
  451. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  452. value |= BIT(BF_SRC_CFGX_SFIFO_ENA);
  453. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  454. aio->streams_on |= PLAYBACK_STREAM_MASK;
  455. break;
  456. case PORT_SPDIF:
  457. value = readl(aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
  458. value |= 0x3;
  459. writel(value, aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
  460. writel(1, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
  461. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  462. value |= BIT(BF_SRC_CFGX_SFIFO_ENA);
  463. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  464. break;
  465. default:
  466. dev_err(aio->cygaud->dev,
  467. "Port not supported %d\n", aio->portnum);
  468. status = -EINVAL;
  469. }
  470. return status;
  471. }
  472. static int audio_ssp_out_disable(struct cygnus_aio_port *aio)
  473. {
  474. u32 value;
  475. int status = 0;
  476. switch (aio->port_type) {
  477. case PORT_TDM:
  478. aio->streams_on &= ~PLAYBACK_STREAM_MASK;
  479. /* If both playback and capture are off */
  480. if (!aio->streams_on) {
  481. value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  482. value &= ~BIT(I2S_OUT_CFGX_CLK_ENA);
  483. value &= ~BIT(I2S_OUT_CFGX_DATA_ENABLE);
  484. writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
  485. }
  486. /* set group_sync_dis = 1 */
  487. value = readl(aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
  488. value |= BIT(aio->portnum);
  489. writel(value, aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
  490. writel(0, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
  491. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  492. value &= ~BIT(BF_SRC_CFGX_SFIFO_ENA);
  493. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  494. /* set group_sync_dis = 0 */
  495. value = readl(aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
  496. value &= ~BIT(aio->portnum);
  497. writel(value, aio->cygaud->audio + BF_SRC_GRP_SYNC_DIS_OFFSET);
  498. value = readl(aio->cygaud->audio + aio->regs.i2s_stream_cfg);
  499. value &= ~BIT(I2S_OUT_STREAM_ENA);
  500. writel(value, aio->cygaud->audio + aio->regs.i2s_stream_cfg);
  501. /* IOP SW INIT on OUT_I2S_x */
  502. value = readl(aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
  503. value |= BIT(aio->portnum);
  504. writel(value, aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
  505. value &= ~BIT(aio->portnum);
  506. writel(value, aio->cygaud->i2s_in + IOP_SW_INIT_LOGIC);
  507. break;
  508. case PORT_SPDIF:
  509. value = readl(aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
  510. value &= ~0x3;
  511. writel(value, aio->cygaud->audio + SPDIF_FORMAT_CFG_OFFSET);
  512. writel(0, aio->cygaud->audio + aio->regs.bf_sourcech_ctrl);
  513. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  514. value &= ~BIT(BF_SRC_CFGX_SFIFO_ENA);
  515. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  516. break;
  517. default:
  518. dev_err(aio->cygaud->dev,
  519. "Port not supported %d\n", aio->portnum);
  520. status = -EINVAL;
  521. }
  522. return status;
  523. }
  524. static int pll_configure_mclk(struct cygnus_audio *cygaud, u32 mclk,
  525. struct cygnus_aio_port *aio)
  526. {
  527. int i = 0, error;
  528. bool found = false;
  529. const struct pll_macro_entry *p_entry;
  530. struct clk *ch_clk;
  531. for (i = 0; i < ARRAY_SIZE(pll_predef_mclk); i++) {
  532. p_entry = &pll_predef_mclk[i];
  533. if (p_entry->mclk == mclk) {
  534. found = true;
  535. break;
  536. }
  537. }
  538. if (!found) {
  539. dev_err(cygaud->dev,
  540. "%s No valid mclk freq (%u) found!\n", __func__, mclk);
  541. return -EINVAL;
  542. }
  543. ch_clk = cygaud->audio_clk[p_entry->pll_ch_num];
  544. if ((aio->clk_trace.cap_en) && (!aio->clk_trace.cap_clk_en)) {
  545. error = clk_prepare_enable(ch_clk);
  546. if (error) {
  547. dev_err(cygaud->dev, "%s clk_prepare_enable failed %d\n",
  548. __func__, error);
  549. return error;
  550. }
  551. aio->clk_trace.cap_clk_en = true;
  552. }
  553. if ((aio->clk_trace.play_en) && (!aio->clk_trace.play_clk_en)) {
  554. error = clk_prepare_enable(ch_clk);
  555. if (error) {
  556. dev_err(cygaud->dev, "%s clk_prepare_enable failed %d\n",
  557. __func__, error);
  558. return error;
  559. }
  560. aio->clk_trace.play_clk_en = true;
  561. }
  562. error = clk_set_rate(ch_clk, mclk);
  563. if (error) {
  564. dev_err(cygaud->dev, "%s Set MCLK rate failed: %d\n",
  565. __func__, error);
  566. return error;
  567. }
  568. return p_entry->pll_ch_num;
  569. }
  570. static int cygnus_ssp_set_clocks(struct cygnus_aio_port *aio,
  571. struct cygnus_audio *cygaud)
  572. {
  573. u32 value, i = 0;
  574. u32 mask = 0xf;
  575. u32 sclk;
  576. bool found = false;
  577. const struct _ssp_clk_coeff *p_entry = NULL;
  578. for (i = 0; i < ARRAY_SIZE(ssp_clk_coeff); i++) {
  579. p_entry = &ssp_clk_coeff[i];
  580. if ((p_entry->rate == aio->lrclk) &&
  581. (p_entry->sclk_rate == aio->bit_per_frame) &&
  582. (p_entry->mclk == aio->mclk)) {
  583. found = true;
  584. break;
  585. }
  586. }
  587. if (!found) {
  588. dev_err(aio->cygaud->dev,
  589. "No valid match found in ssp_clk_coeff array\n");
  590. dev_err(aio->cygaud->dev, "lrclk = %u, bits/frame = %u, mclk = %u\n",
  591. aio->lrclk, aio->bit_per_frame, aio->mclk);
  592. return -EINVAL;
  593. }
  594. sclk = aio->bit_per_frame;
  595. if (sclk == 512)
  596. sclk = 0;
  597. /* sclks_per_1fs_div = sclk cycles/32 */
  598. sclk /= 32;
  599. /* Set sclk rate */
  600. switch (aio->port_type) {
  601. case PORT_TDM:
  602. /* Set number of bitclks per frame */
  603. value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  604. value &= ~(mask << I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32);
  605. value |= sclk << I2S_OUT_CFGX_SCLKS_PER_1FS_DIV32;
  606. writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
  607. dev_dbg(aio->cygaud->dev,
  608. "SCLKS_PER_1FS_DIV32 = 0x%x\n", value);
  609. break;
  610. case PORT_SPDIF:
  611. break;
  612. default:
  613. dev_err(aio->cygaud->dev, "Unknown port type\n");
  614. return -EINVAL;
  615. }
  616. /* Set MCLK_RATE ssp port (spdif and ssp are the same) */
  617. value = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
  618. value &= ~(0xf << I2S_OUT_MCLKRATE_SHIFT);
  619. value |= (p_entry->mclk_rate << I2S_OUT_MCLKRATE_SHIFT);
  620. writel(value, aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
  621. dev_dbg(aio->cygaud->dev, "mclk cfg reg = 0x%x\n", value);
  622. dev_dbg(aio->cygaud->dev, "bits per frame = %u, mclk = %u Hz, lrclk = %u Hz\n",
  623. aio->bit_per_frame, aio->mclk, aio->lrclk);
  624. return 0;
  625. }
  626. static int cygnus_ssp_hw_params(struct snd_pcm_substream *substream,
  627. struct snd_pcm_hw_params *params,
  628. struct snd_soc_dai *dai)
  629. {
  630. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
  631. struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
  632. int rate, bitres;
  633. u32 value;
  634. u32 mask = 0x1f;
  635. int ret = 0;
  636. dev_dbg(aio->cygaud->dev, "%s port = %d\n", __func__, aio->portnum);
  637. dev_dbg(aio->cygaud->dev, "params_channels %d\n",
  638. params_channels(params));
  639. dev_dbg(aio->cygaud->dev, "rate %d\n", params_rate(params));
  640. dev_dbg(aio->cygaud->dev, "format %d\n", params_format(params));
  641. rate = params_rate(params);
  642. switch (aio->mode) {
  643. case CYGNUS_SSPMODE_TDM:
  644. if ((rate == 192000) && (params_channels(params) > 4)) {
  645. dev_err(aio->cygaud->dev, "Cannot run %d channels at %dHz\n",
  646. params_channels(params), rate);
  647. return -EINVAL;
  648. }
  649. break;
  650. case CYGNUS_SSPMODE_I2S:
  651. aio->bit_per_frame = 64; /* I2S must be 64 bit per frame */
  652. break;
  653. default:
  654. dev_err(aio->cygaud->dev,
  655. "%s port running in unknown mode\n", __func__);
  656. return -EINVAL;
  657. }
  658. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  659. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  660. value &= ~BIT(BF_SRC_CFGX_BUFFER_PAIR_ENABLE);
  661. /* Configure channels as mono or stereo/TDM */
  662. if (params_channels(params) == 1)
  663. value |= BIT(BF_SRC_CFGX_SAMPLE_CH_MODE);
  664. else
  665. value &= ~BIT(BF_SRC_CFGX_SAMPLE_CH_MODE);
  666. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  667. switch (params_format(params)) {
  668. case SNDRV_PCM_FORMAT_S8:
  669. if (aio->port_type == PORT_SPDIF) {
  670. dev_err(aio->cygaud->dev,
  671. "SPDIF does not support 8bit format\n");
  672. return -EINVAL;
  673. }
  674. bitres = 8;
  675. break;
  676. case SNDRV_PCM_FORMAT_S16_LE:
  677. bitres = 16;
  678. break;
  679. case SNDRV_PCM_FORMAT_S32_LE:
  680. /* 32 bit mode is coded as 0 */
  681. bitres = 0;
  682. break;
  683. default:
  684. return -EINVAL;
  685. }
  686. value = readl(aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  687. value &= ~(mask << BF_SRC_CFGX_BIT_RES);
  688. value |= (bitres << BF_SRC_CFGX_BIT_RES);
  689. writel(value, aio->cygaud->audio + aio->regs.bf_sourcech_cfg);
  690. } else {
  691. switch (params_format(params)) {
  692. case SNDRV_PCM_FORMAT_S16_LE:
  693. value = readl(aio->cygaud->audio +
  694. aio->regs.bf_destch_cfg);
  695. value |= BIT(BF_DST_CFGX_CAP_MODE);
  696. writel(value, aio->cygaud->audio +
  697. aio->regs.bf_destch_cfg);
  698. break;
  699. case SNDRV_PCM_FORMAT_S32_LE:
  700. value = readl(aio->cygaud->audio +
  701. aio->regs.bf_destch_cfg);
  702. value &= ~BIT(BF_DST_CFGX_CAP_MODE);
  703. writel(value, aio->cygaud->audio +
  704. aio->regs.bf_destch_cfg);
  705. break;
  706. default:
  707. return -EINVAL;
  708. }
  709. }
  710. aio->lrclk = rate;
  711. if (!aio->is_slave)
  712. ret = cygnus_ssp_set_clocks(aio, cygaud);
  713. return ret;
  714. }
  715. /*
  716. * This function sets the mclk frequency for pll clock
  717. */
  718. static int cygnus_ssp_set_sysclk(struct snd_soc_dai *dai,
  719. int clk_id, unsigned int freq, int dir)
  720. {
  721. int sel;
  722. u32 value;
  723. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
  724. struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
  725. dev_dbg(aio->cygaud->dev,
  726. "%s Enter port = %d\n", __func__, aio->portnum);
  727. sel = pll_configure_mclk(cygaud, freq, aio);
  728. if (sel < 0) {
  729. dev_err(aio->cygaud->dev,
  730. "%s Setting mclk failed.\n", __func__);
  731. return -EINVAL;
  732. }
  733. aio->mclk = freq;
  734. dev_dbg(aio->cygaud->dev, "%s Setting MCLKSEL to %d\n", __func__, sel);
  735. value = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
  736. value &= ~(0xf << I2S_OUT_PLLCLKSEL_SHIFT);
  737. value |= (sel << I2S_OUT_PLLCLKSEL_SHIFT);
  738. writel(value, aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
  739. return 0;
  740. }
  741. static int cygnus_ssp_startup(struct snd_pcm_substream *substream,
  742. struct snd_soc_dai *dai)
  743. {
  744. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
  745. snd_soc_dai_set_dma_data(dai, substream, aio);
  746. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  747. aio->clk_trace.play_en = true;
  748. else
  749. aio->clk_trace.cap_en = true;
  750. return 0;
  751. }
  752. static void cygnus_ssp_shutdown(struct snd_pcm_substream *substream,
  753. struct snd_soc_dai *dai)
  754. {
  755. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
  756. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  757. aio->clk_trace.play_en = false;
  758. else
  759. aio->clk_trace.cap_en = false;
  760. if (!aio->is_slave) {
  761. u32 val;
  762. val = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
  763. val &= CYGNUS_PLLCLKSEL_MASK;
  764. if (val >= ARRAY_SIZE(aio->cygaud->audio_clk)) {
  765. dev_err(aio->cygaud->dev, "Clk index %u is out of bounds\n",
  766. val);
  767. return;
  768. }
  769. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  770. if (aio->clk_trace.play_clk_en) {
  771. clk_disable_unprepare(aio->cygaud->
  772. audio_clk[val]);
  773. aio->clk_trace.play_clk_en = false;
  774. }
  775. } else {
  776. if (aio->clk_trace.cap_clk_en) {
  777. clk_disable_unprepare(aio->cygaud->
  778. audio_clk[val]);
  779. aio->clk_trace.cap_clk_en = false;
  780. }
  781. }
  782. }
  783. }
  784. /*
  785. * Bit Update Notes
  786. * 31 Yes TDM Mode (1 = TDM, 0 = i2s)
  787. * 30 Yes Slave Mode (1 = Slave, 0 = Master)
  788. * 29:26 No Sclks per frame
  789. * 25:18 Yes FS Width
  790. * 17:14 No Valid Slots
  791. * 13 No Bits (1 = 16 bits, 0 = 32 bits)
  792. * 12:08 No Bits per samp
  793. * 07 Yes Justifcation (1 = LSB, 0 = MSB)
  794. * 06 Yes Alignment (1 = Delay 1 clk, 0 = no delay
  795. * 05 Yes SCLK polarity (1 = Rising, 0 = Falling)
  796. * 04 Yes LRCLK Polarity (1 = High for left, 0 = Low for left)
  797. * 03:02 Yes Reserved - write as zero
  798. * 01 No Data Enable
  799. * 00 No CLK Enable
  800. */
  801. #define I2S_OUT_CFG_REG_UPDATE_MASK 0x3C03FF03
  802. /* Input cfg is same as output, but the FS width is not a valid field */
  803. #define I2S_IN_CFG_REG_UPDATE_MASK (I2S_OUT_CFG_REG_UPDATE_MASK | 0x03FC0000)
  804. int cygnus_ssp_set_custom_fsync_width(struct snd_soc_dai *cpu_dai, int len)
  805. {
  806. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
  807. if ((len > 0) && (len < 256)) {
  808. aio->fsync_width = len;
  809. return 0;
  810. } else {
  811. return -EINVAL;
  812. }
  813. }
  814. static int cygnus_ssp_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  815. {
  816. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
  817. u32 ssp_curcfg;
  818. u32 ssp_newcfg;
  819. u32 ssp_outcfg;
  820. u32 ssp_incfg;
  821. u32 val;
  822. u32 mask;
  823. dev_dbg(aio->cygaud->dev, "%s Enter fmt: %x\n", __func__, fmt);
  824. if (aio->port_type == PORT_SPDIF)
  825. return -EINVAL;
  826. ssp_newcfg = 0;
  827. switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
  828. case SND_SOC_DAIFMT_CBM_CFM:
  829. ssp_newcfg |= BIT(I2S_OUT_CFGX_SLAVE_MODE);
  830. aio->is_slave = 1;
  831. break;
  832. case SND_SOC_DAIFMT_CBS_CFS:
  833. ssp_newcfg &= ~BIT(I2S_OUT_CFGX_SLAVE_MODE);
  834. aio->is_slave = 0;
  835. break;
  836. default:
  837. return -EINVAL;
  838. }
  839. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  840. case SND_SOC_DAIFMT_I2S:
  841. ssp_newcfg |= BIT(I2S_OUT_CFGX_DATA_ALIGNMENT);
  842. ssp_newcfg |= BIT(I2S_OUT_CFGX_FSYNC_WIDTH);
  843. aio->mode = CYGNUS_SSPMODE_I2S;
  844. break;
  845. case SND_SOC_DAIFMT_DSP_A:
  846. case SND_SOC_DAIFMT_DSP_B:
  847. ssp_newcfg |= BIT(I2S_OUT_CFGX_TDM_MODE);
  848. /* DSP_A = data after FS, DSP_B = data during FS */
  849. if ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) == SND_SOC_DAIFMT_DSP_A)
  850. ssp_newcfg |= BIT(I2S_OUT_CFGX_DATA_ALIGNMENT);
  851. if ((aio->fsync_width > 0) && (aio->fsync_width < 256))
  852. ssp_newcfg |=
  853. (aio->fsync_width << I2S_OUT_CFGX_FSYNC_WIDTH);
  854. else
  855. ssp_newcfg |= BIT(I2S_OUT_CFGX_FSYNC_WIDTH);
  856. aio->mode = CYGNUS_SSPMODE_TDM;
  857. break;
  858. default:
  859. return -EINVAL;
  860. }
  861. /*
  862. * SSP out cfg.
  863. * Retain bits we do not want to update, then OR in new bits
  864. */
  865. ssp_curcfg = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  866. ssp_outcfg = (ssp_curcfg & I2S_OUT_CFG_REG_UPDATE_MASK) | ssp_newcfg;
  867. writel(ssp_outcfg, aio->cygaud->audio + aio->regs.i2s_cfg);
  868. /*
  869. * SSP in cfg.
  870. * Retain bits we do not want to update, then OR in new bits
  871. */
  872. ssp_curcfg = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
  873. ssp_incfg = (ssp_curcfg & I2S_IN_CFG_REG_UPDATE_MASK) | ssp_newcfg;
  874. writel(ssp_incfg, aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
  875. val = readl(aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  876. /*
  877. * Configure the word clk and bit clk as output or tristate
  878. * Each port has 4 bits for controlling its pins.
  879. * Shift the mask based upon port number.
  880. */
  881. mask = BIT(AUD_MISC_SEROUT_LRCK_OE)
  882. | BIT(AUD_MISC_SEROUT_SCLK_OE)
  883. | BIT(AUD_MISC_SEROUT_MCLK_OE);
  884. mask = mask << (aio->portnum * 4);
  885. if (aio->is_slave)
  886. /* Set bit for tri-state */
  887. val |= mask;
  888. else
  889. /* Clear bit for drive */
  890. val &= ~mask;
  891. dev_dbg(aio->cygaud->dev, "%s Set OE bits 0x%x\n", __func__, val);
  892. writel(val, aio->cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  893. return 0;
  894. }
  895. static int cygnus_ssp_trigger(struct snd_pcm_substream *substream, int cmd,
  896. struct snd_soc_dai *dai)
  897. {
  898. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(dai);
  899. struct cygnus_audio *cygaud = snd_soc_dai_get_drvdata(dai);
  900. dev_dbg(aio->cygaud->dev,
  901. "%s cmd %d at port = %d\n", __func__, cmd, aio->portnum);
  902. switch (cmd) {
  903. case SNDRV_PCM_TRIGGER_START:
  904. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  905. case SNDRV_PCM_TRIGGER_RESUME:
  906. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  907. audio_ssp_out_enable(aio);
  908. else
  909. audio_ssp_in_enable(aio);
  910. cygaud->active_ports++;
  911. break;
  912. case SNDRV_PCM_TRIGGER_STOP:
  913. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  914. case SNDRV_PCM_TRIGGER_SUSPEND:
  915. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  916. audio_ssp_out_disable(aio);
  917. else
  918. audio_ssp_in_disable(aio);
  919. cygaud->active_ports--;
  920. break;
  921. default:
  922. return -EINVAL;
  923. }
  924. return 0;
  925. }
  926. static int cygnus_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai,
  927. unsigned int tx_mask, unsigned int rx_mask, int slots, int slot_width)
  928. {
  929. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
  930. u32 value;
  931. int bits_per_slot = 0; /* default to 32-bits per slot */
  932. int frame_bits;
  933. unsigned int active_slots;
  934. bool found = false;
  935. int i;
  936. if (tx_mask != rx_mask) {
  937. dev_err(aio->cygaud->dev,
  938. "%s tx_mask must equal rx_mask\n", __func__);
  939. return -EINVAL;
  940. }
  941. active_slots = hweight32(tx_mask);
  942. if ((active_slots < 0) || (active_slots > 16))
  943. return -EINVAL;
  944. /* Slot value must be even */
  945. if (active_slots % 2)
  946. return -EINVAL;
  947. /* We encode 16 slots as 0 in the reg */
  948. if (active_slots == 16)
  949. active_slots = 0;
  950. /* Slot Width is either 16 or 32 */
  951. switch (slot_width) {
  952. case 16:
  953. bits_per_slot = 1;
  954. break;
  955. case 32:
  956. bits_per_slot = 0;
  957. break;
  958. default:
  959. bits_per_slot = 0;
  960. dev_warn(aio->cygaud->dev,
  961. "%s Defaulting Slot Width to 32\n", __func__);
  962. }
  963. frame_bits = slots * slot_width;
  964. for (i = 0; i < ARRAY_SIZE(ssp_valid_tdm_framesize); i++) {
  965. if (ssp_valid_tdm_framesize[i] == frame_bits) {
  966. found = true;
  967. break;
  968. }
  969. }
  970. if (!found) {
  971. dev_err(aio->cygaud->dev,
  972. "%s In TDM mode, frame bits INVALID (%d)\n",
  973. __func__, frame_bits);
  974. return -EINVAL;
  975. }
  976. aio->bit_per_frame = frame_bits;
  977. dev_dbg(aio->cygaud->dev, "%s active_slots %u, bits per frame %d\n",
  978. __func__, active_slots, frame_bits);
  979. /* Set capture side of ssp port */
  980. value = readl(aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
  981. value &= ~(0xf << I2S_OUT_CFGX_VALID_SLOT);
  982. value |= (active_slots << I2S_OUT_CFGX_VALID_SLOT);
  983. value &= ~BIT(I2S_OUT_CFGX_BITS_PER_SLOT);
  984. value |= (bits_per_slot << I2S_OUT_CFGX_BITS_PER_SLOT);
  985. writel(value, aio->cygaud->i2s_in + aio->regs.i2s_cap_cfg);
  986. /* Set playback side of ssp port */
  987. value = readl(aio->cygaud->audio + aio->regs.i2s_cfg);
  988. value &= ~(0xf << I2S_OUT_CFGX_VALID_SLOT);
  989. value |= (active_slots << I2S_OUT_CFGX_VALID_SLOT);
  990. value &= ~BIT(I2S_OUT_CFGX_BITS_PER_SLOT);
  991. value |= (bits_per_slot << I2S_OUT_CFGX_BITS_PER_SLOT);
  992. writel(value, aio->cygaud->audio + aio->regs.i2s_cfg);
  993. return 0;
  994. }
  995. #ifdef CONFIG_PM_SLEEP
  996. static int cygnus_ssp_suspend(struct snd_soc_dai *cpu_dai)
  997. {
  998. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
  999. if (!aio->is_slave) {
  1000. u32 val;
  1001. val = readl(aio->cygaud->audio + aio->regs.i2s_mclk_cfg);
  1002. val &= CYGNUS_PLLCLKSEL_MASK;
  1003. if (val >= ARRAY_SIZE(aio->cygaud->audio_clk)) {
  1004. dev_err(aio->cygaud->dev, "Clk index %u is out of bounds\n",
  1005. val);
  1006. return -EINVAL;
  1007. }
  1008. if (aio->clk_trace.cap_clk_en)
  1009. clk_disable_unprepare(aio->cygaud->audio_clk[val]);
  1010. if (aio->clk_trace.play_clk_en)
  1011. clk_disable_unprepare(aio->cygaud->audio_clk[val]);
  1012. aio->pll_clk_num = val;
  1013. }
  1014. return 0;
  1015. }
  1016. static int cygnus_ssp_resume(struct snd_soc_dai *cpu_dai)
  1017. {
  1018. struct cygnus_aio_port *aio = cygnus_dai_get_portinfo(cpu_dai);
  1019. int error;
  1020. if (!aio->is_slave) {
  1021. if (aio->clk_trace.cap_clk_en) {
  1022. error = clk_prepare_enable(aio->cygaud->
  1023. audio_clk[aio->pll_clk_num]);
  1024. if (error) {
  1025. dev_err(aio->cygaud->dev, "%s clk_prepare_enable failed\n",
  1026. __func__);
  1027. return -EINVAL;
  1028. }
  1029. }
  1030. if (aio->clk_trace.play_clk_en) {
  1031. error = clk_prepare_enable(aio->cygaud->
  1032. audio_clk[aio->pll_clk_num]);
  1033. if (error) {
  1034. if (aio->clk_trace.cap_clk_en)
  1035. clk_disable_unprepare(aio->cygaud->
  1036. audio_clk[aio->pll_clk_num]);
  1037. dev_err(aio->cygaud->dev, "%s clk_prepare_enable failed\n",
  1038. __func__);
  1039. return -EINVAL;
  1040. }
  1041. }
  1042. }
  1043. return 0;
  1044. }
  1045. #else
  1046. #define cygnus_ssp_suspend NULL
  1047. #define cygnus_ssp_resume NULL
  1048. #endif
  1049. static const struct snd_soc_dai_ops cygnus_ssp_dai_ops = {
  1050. .startup = cygnus_ssp_startup,
  1051. .shutdown = cygnus_ssp_shutdown,
  1052. .trigger = cygnus_ssp_trigger,
  1053. .hw_params = cygnus_ssp_hw_params,
  1054. .set_fmt = cygnus_ssp_set_fmt,
  1055. .set_sysclk = cygnus_ssp_set_sysclk,
  1056. .set_tdm_slot = cygnus_set_dai_tdm_slot,
  1057. };
  1058. #define INIT_CPU_DAI(num) { \
  1059. .name = "cygnus-ssp" #num, \
  1060. .playback = { \
  1061. .channels_min = 1, \
  1062. .channels_max = 16, \
  1063. .rates = CYGNUS_TDM_RATE | SNDRV_PCM_RATE_88200 | \
  1064. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
  1065. SNDRV_PCM_RATE_192000, \
  1066. .formats = SNDRV_PCM_FMTBIT_S8 | \
  1067. SNDRV_PCM_FMTBIT_S16_LE | \
  1068. SNDRV_PCM_FMTBIT_S32_LE, \
  1069. }, \
  1070. .capture = { \
  1071. .channels_min = 2, \
  1072. .channels_max = 16, \
  1073. .rates = CYGNUS_TDM_RATE | SNDRV_PCM_RATE_88200 | \
  1074. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \
  1075. SNDRV_PCM_RATE_192000, \
  1076. .formats = SNDRV_PCM_FMTBIT_S16_LE | \
  1077. SNDRV_PCM_FMTBIT_S32_LE, \
  1078. }, \
  1079. .ops = &cygnus_ssp_dai_ops, \
  1080. .suspend = cygnus_ssp_suspend, \
  1081. .resume = cygnus_ssp_resume, \
  1082. }
  1083. static const struct snd_soc_dai_driver cygnus_ssp_dai_info[] = {
  1084. INIT_CPU_DAI(0),
  1085. INIT_CPU_DAI(1),
  1086. INIT_CPU_DAI(2),
  1087. };
  1088. static struct snd_soc_dai_driver cygnus_spdif_dai_info = {
  1089. .name = "cygnus-spdif",
  1090. .playback = {
  1091. .channels_min = 2,
  1092. .channels_max = 2,
  1093. .rates = CYGNUS_TDM_RATE | SNDRV_PCM_RATE_88200 |
  1094. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  1095. SNDRV_PCM_RATE_192000,
  1096. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  1097. SNDRV_PCM_FMTBIT_S32_LE,
  1098. },
  1099. .ops = &cygnus_ssp_dai_ops,
  1100. .suspend = cygnus_ssp_suspend,
  1101. .resume = cygnus_ssp_resume,
  1102. };
  1103. static struct snd_soc_dai_driver cygnus_ssp_dai[CYGNUS_MAX_PORTS];
  1104. static const struct snd_soc_component_driver cygnus_ssp_component = {
  1105. .name = "cygnus-audio",
  1106. };
  1107. /*
  1108. * Return < 0 if error
  1109. * Return 0 if disabled
  1110. * Return 1 if enabled and node is parsed successfully
  1111. */
  1112. static int parse_ssp_child_node(struct platform_device *pdev,
  1113. struct device_node *dn,
  1114. struct cygnus_audio *cygaud,
  1115. struct snd_soc_dai_driver *p_dai)
  1116. {
  1117. struct cygnus_aio_port *aio;
  1118. struct cygnus_ssp_regs ssp_regs[3];
  1119. u32 rawval;
  1120. int portnum = -1;
  1121. enum cygnus_audio_port_type port_type;
  1122. if (of_property_read_u32(dn, "reg", &rawval)) {
  1123. dev_err(&pdev->dev, "Missing reg property\n");
  1124. return -EINVAL;
  1125. }
  1126. portnum = rawval;
  1127. switch (rawval) {
  1128. case 0:
  1129. ssp_regs[0] = INIT_SSP_REGS(0);
  1130. port_type = PORT_TDM;
  1131. break;
  1132. case 1:
  1133. ssp_regs[1] = INIT_SSP_REGS(1);
  1134. port_type = PORT_TDM;
  1135. break;
  1136. case 2:
  1137. ssp_regs[2] = INIT_SSP_REGS(2);
  1138. port_type = PORT_TDM;
  1139. break;
  1140. case 3:
  1141. port_type = PORT_SPDIF;
  1142. break;
  1143. default:
  1144. dev_err(&pdev->dev, "Bad value for reg %u\n", rawval);
  1145. return -EINVAL;
  1146. }
  1147. aio = &cygaud->portinfo[portnum];
  1148. aio->cygaud = cygaud;
  1149. aio->portnum = portnum;
  1150. aio->port_type = port_type;
  1151. aio->fsync_width = -1;
  1152. switch (port_type) {
  1153. case PORT_TDM:
  1154. aio->regs = ssp_regs[portnum];
  1155. *p_dai = cygnus_ssp_dai_info[portnum];
  1156. aio->mode = CYGNUS_SSPMODE_UNKNOWN;
  1157. break;
  1158. case PORT_SPDIF:
  1159. aio->regs.bf_sourcech_cfg = BF_SRC_CFG3_OFFSET;
  1160. aio->regs.bf_sourcech_ctrl = BF_SRC_CTRL3_OFFSET;
  1161. aio->regs.i2s_mclk_cfg = SPDIF_MCLK_CFG_OFFSET;
  1162. aio->regs.i2s_stream_cfg = SPDIF_STREAM_CFG_OFFSET;
  1163. *p_dai = cygnus_spdif_dai_info;
  1164. /* For the purposes of this code SPDIF can be I2S mode */
  1165. aio->mode = CYGNUS_SSPMODE_I2S;
  1166. break;
  1167. default:
  1168. dev_err(&pdev->dev, "Bad value for port_type %d\n", port_type);
  1169. return -EINVAL;
  1170. }
  1171. dev_dbg(&pdev->dev, "%s portnum = %d\n", __func__, aio->portnum);
  1172. aio->streams_on = 0;
  1173. aio->cygaud->dev = &pdev->dev;
  1174. aio->clk_trace.play_en = false;
  1175. aio->clk_trace.cap_en = false;
  1176. audio_ssp_init_portregs(aio);
  1177. return 0;
  1178. }
  1179. static int audio_clk_init(struct platform_device *pdev,
  1180. struct cygnus_audio *cygaud)
  1181. {
  1182. int i;
  1183. char clk_name[PROP_LEN_MAX];
  1184. for (i = 0; i < ARRAY_SIZE(cygaud->audio_clk); i++) {
  1185. snprintf(clk_name, PROP_LEN_MAX, "ch%d_audio", i);
  1186. cygaud->audio_clk[i] = devm_clk_get(&pdev->dev, clk_name);
  1187. if (IS_ERR(cygaud->audio_clk[i]))
  1188. return PTR_ERR(cygaud->audio_clk[i]);
  1189. }
  1190. return 0;
  1191. }
  1192. static int cygnus_ssp_probe(struct platform_device *pdev)
  1193. {
  1194. struct device *dev = &pdev->dev;
  1195. struct device_node *child_node;
  1196. struct resource *res = pdev->resource;
  1197. struct cygnus_audio *cygaud;
  1198. int err = -EINVAL;
  1199. int node_count;
  1200. int active_port_count;
  1201. cygaud = devm_kzalloc(dev, sizeof(struct cygnus_audio), GFP_KERNEL);
  1202. if (!cygaud)
  1203. return -ENOMEM;
  1204. dev_set_drvdata(dev, cygaud);
  1205. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aud");
  1206. cygaud->audio = devm_ioremap_resource(dev, res);
  1207. if (IS_ERR(cygaud->audio))
  1208. return PTR_ERR(cygaud->audio);
  1209. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "i2s_in");
  1210. cygaud->i2s_in = devm_ioremap_resource(dev, res);
  1211. if (IS_ERR(cygaud->i2s_in))
  1212. return PTR_ERR(cygaud->i2s_in);
  1213. /* Tri-state all controlable pins until we know that we need them */
  1214. writel(CYGNUS_SSP_TRISTATE_MASK,
  1215. cygaud->audio + AUD_MISC_SEROUT_OE_REG_BASE);
  1216. node_count = of_get_child_count(pdev->dev.of_node);
  1217. if ((node_count < 1) || (node_count > CYGNUS_MAX_PORTS)) {
  1218. dev_err(dev, "child nodes is %d. Must be between 1 and %d\n",
  1219. node_count, CYGNUS_MAX_PORTS);
  1220. return -EINVAL;
  1221. }
  1222. active_port_count = 0;
  1223. for_each_available_child_of_node(pdev->dev.of_node, child_node) {
  1224. err = parse_ssp_child_node(pdev, child_node, cygaud,
  1225. &cygnus_ssp_dai[active_port_count]);
  1226. /* negative is err, 0 is active and good, 1 is disabled */
  1227. if (err < 0)
  1228. return err;
  1229. else if (!err) {
  1230. dev_dbg(dev, "Activating DAI: %s\n",
  1231. cygnus_ssp_dai[active_port_count].name);
  1232. active_port_count++;
  1233. }
  1234. }
  1235. cygaud->dev = dev;
  1236. cygaud->active_ports = 0;
  1237. dev_dbg(dev, "Registering %d DAIs\n", active_port_count);
  1238. err = snd_soc_register_component(dev, &cygnus_ssp_component,
  1239. cygnus_ssp_dai, active_port_count);
  1240. if (err) {
  1241. dev_err(dev, "snd_soc_register_dai failed\n");
  1242. return err;
  1243. }
  1244. cygaud->irq_num = platform_get_irq(pdev, 0);
  1245. if (cygaud->irq_num <= 0) {
  1246. dev_err(dev, "platform_get_irq failed\n");
  1247. err = cygaud->irq_num;
  1248. goto err_irq;
  1249. }
  1250. err = audio_clk_init(pdev, cygaud);
  1251. if (err) {
  1252. dev_err(dev, "audio clock initialization failed\n");
  1253. goto err_irq;
  1254. }
  1255. err = cygnus_soc_platform_register(dev, cygaud);
  1256. if (err) {
  1257. dev_err(dev, "platform reg error %d\n", err);
  1258. goto err_irq;
  1259. }
  1260. return 0;
  1261. err_irq:
  1262. snd_soc_unregister_component(dev);
  1263. return err;
  1264. }
  1265. static int cygnus_ssp_remove(struct platform_device *pdev)
  1266. {
  1267. cygnus_soc_platform_unregister(&pdev->dev);
  1268. snd_soc_unregister_component(&pdev->dev);
  1269. return 0;
  1270. }
  1271. static const struct of_device_id cygnus_ssp_of_match[] = {
  1272. { .compatible = "brcm,cygnus-audio" },
  1273. {},
  1274. };
  1275. MODULE_DEVICE_TABLE(of, cygnus_ssp_of_match);
  1276. static struct platform_driver cygnus_ssp_driver = {
  1277. .probe = cygnus_ssp_probe,
  1278. .remove = cygnus_ssp_remove,
  1279. .driver = {
  1280. .name = "cygnus-ssp",
  1281. .of_match_table = cygnus_ssp_of_match,
  1282. },
  1283. };
  1284. module_platform_driver(cygnus_ssp_driver);
  1285. MODULE_ALIAS("platform:cygnus-ssp");
  1286. MODULE_LICENSE("GPL v2");
  1287. MODULE_AUTHOR("Broadcom");
  1288. MODULE_DESCRIPTION("Cygnus ASoC SSP Interface");