max2175.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Maxim Integrated MAX2175 RF to Bits tuner driver
  4. *
  5. * This driver & most of the hard coded values are based on the reference
  6. * application delivered by Maxim for this device.
  7. *
  8. * Copyright (C) 2016 Maxim Integrated Products
  9. * Copyright (C) 2017 Renesas Electronics Corporation
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/errno.h>
  14. #include <linux/i2c.h>
  15. #include <linux/kernel.h>
  16. #include <linux/math64.h>
  17. #include <linux/max2175.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-device.h>
  24. #include "max2175.h"
  25. #define DRIVER_NAME "max2175"
  26. #define mxm_dbg(ctx, fmt, arg...) dev_dbg(&ctx->client->dev, fmt, ## arg)
  27. #define mxm_err(ctx, fmt, arg...) dev_err(&ctx->client->dev, fmt, ## arg)
  28. /* Rx mode */
  29. struct max2175_rxmode {
  30. enum max2175_band band; /* Associated band */
  31. u32 freq; /* Default freq in Hz */
  32. u8 i2s_word_size; /* Bit value */
  33. };
  34. /* Register map to define preset values */
  35. struct max2175_reg_map {
  36. u8 idx; /* Register index */
  37. u8 val; /* Register value */
  38. };
  39. static const struct max2175_rxmode eu_rx_modes[] = {
  40. /* EU modes */
  41. [MAX2175_EU_FM_1_2] = { MAX2175_BAND_FM, 98256000, 1 },
  42. [MAX2175_DAB_1_2] = { MAX2175_BAND_VHF, 182640000, 0 },
  43. };
  44. static const struct max2175_rxmode na_rx_modes[] = {
  45. /* NA modes */
  46. [MAX2175_NA_FM_1_0] = { MAX2175_BAND_FM, 98255520, 1 },
  47. [MAX2175_NA_FM_2_0] = { MAX2175_BAND_FM, 98255520, 6 },
  48. };
  49. /*
  50. * Preset values:
  51. * Based on Maxim MAX2175 Register Table revision: 130p10
  52. */
  53. static const u8 full_fm_eu_1p0[] = {
  54. 0x15, 0x04, 0xb8, 0xe3, 0x35, 0x18, 0x7c, 0x00,
  55. 0x00, 0x7d, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
  56. 0x61, 0x61, 0x61, 0x61, 0x5a, 0x0f, 0x34, 0x1c,
  57. 0x14, 0x88, 0x33, 0x02, 0x00, 0x09, 0x00, 0x65,
  58. 0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
  60. 0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0x2f, 0x7e, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
  63. 0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
  64. 0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
  66. 0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
  67. 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0xac, 0x40, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
  70. 0x00, 0x47, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
  71. 0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
  72. 0x1b,
  73. };
  74. static const u8 full_fm_na_1p0[] = {
  75. 0x13, 0x08, 0x8d, 0xc0, 0x35, 0x18, 0x7d, 0x3f,
  76. 0x7d, 0x75, 0x40, 0x08, 0x70, 0x7a, 0x88, 0x91,
  77. 0x61, 0x61, 0x61, 0x61, 0x5c, 0x0f, 0x34, 0x1c,
  78. 0x14, 0x88, 0x33, 0x02, 0x00, 0x01, 0x00, 0x65,
  79. 0x9f, 0x2b, 0x80, 0x00, 0x95, 0x05, 0x2c, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
  81. 0x4a, 0x08, 0xa8, 0x0e, 0x0e, 0xaf, 0x7e, 0x00,
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x5e, 0xa9,
  84. 0xae, 0xbb, 0x57, 0x18, 0x3b, 0x03, 0x3b, 0x64,
  85. 0x40, 0x60, 0x00, 0x2a, 0xbf, 0x3f, 0xff, 0x9f,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00,
  87. 0xff, 0xfc, 0xef, 0x1c, 0x40, 0x00, 0x00, 0x02,
  88. 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00,
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, 0x40, 0x00,
  90. 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00,
  91. 0x00, 0x35, 0x00, 0x00, 0x11, 0x3f, 0x22, 0x00,
  92. 0xf1, 0x00, 0x41, 0x03, 0xb0, 0x00, 0x00, 0x00,
  93. 0x1b,
  94. };
  95. /* DAB1.2 settings */
  96. static const struct max2175_reg_map dab12_map[] = {
  97. { 0x01, 0x13 }, { 0x02, 0x0d }, { 0x03, 0x15 }, { 0x04, 0x55 },
  98. { 0x05, 0x0a }, { 0x06, 0xa0 }, { 0x07, 0x40 }, { 0x08, 0x00 },
  99. { 0x09, 0x00 }, { 0x0a, 0x7d }, { 0x0b, 0x4a }, { 0x0c, 0x28 },
  100. { 0x0e, 0x43 }, { 0x0f, 0xb5 }, { 0x10, 0x31 }, { 0x11, 0x9e },
  101. { 0x12, 0x68 }, { 0x13, 0x9e }, { 0x14, 0x68 }, { 0x15, 0x58 },
  102. { 0x16, 0x2f }, { 0x17, 0x3f }, { 0x18, 0x40 }, { 0x1a, 0x88 },
  103. { 0x1b, 0xaa }, { 0x1c, 0x9a }, { 0x1d, 0x00 }, { 0x1e, 0x00 },
  104. { 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x00 }, { 0x26, 0x00 },
  105. { 0x27, 0x00 }, { 0x32, 0x08 }, { 0x33, 0xf8 }, { 0x36, 0x2d },
  106. { 0x37, 0x7e }, { 0x55, 0xaf }, { 0x56, 0x3f }, { 0x57, 0xf8 },
  107. { 0x58, 0x99 }, { 0x76, 0x00 }, { 0x77, 0x00 }, { 0x78, 0x02 },
  108. { 0x79, 0x40 }, { 0x82, 0x00 }, { 0x83, 0x00 }, { 0x85, 0x00 },
  109. { 0x86, 0x20 },
  110. };
  111. /* EU FM 1.2 settings */
  112. static const struct max2175_reg_map fmeu1p2_map[] = {
  113. { 0x01, 0x15 }, { 0x02, 0x04 }, { 0x03, 0xb8 }, { 0x04, 0xe3 },
  114. { 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x00 },
  115. { 0x09, 0x00 }, { 0x0a, 0x73 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
  116. { 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
  117. { 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5a },
  118. { 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
  119. { 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
  120. { 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
  121. { 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0x2f },
  122. { 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
  123. { 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0x40 }, { 0x78, 0x00 },
  124. { 0x79, 0x00 }, { 0x82, 0x47 }, { 0x83, 0x00 }, { 0x85, 0x11 },
  125. { 0x86, 0x3f },
  126. };
  127. /* FM NA 1.0 settings */
  128. static const struct max2175_reg_map fmna1p0_map[] = {
  129. { 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
  130. { 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7d }, { 0x08, 0x3f },
  131. { 0x09, 0x7d }, { 0x0a, 0x75 }, { 0x0b, 0x40 }, { 0x0c, 0x08 },
  132. { 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
  133. { 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
  134. { 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
  135. { 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
  136. { 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
  137. { 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
  138. { 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
  139. { 0x58, 0x9f }, { 0x76, 0xa6 }, { 0x77, 0x40 }, { 0x78, 0x00 },
  140. { 0x79, 0x00 }, { 0x82, 0x35 }, { 0x83, 0x00 }, { 0x85, 0x11 },
  141. { 0x86, 0x3f },
  142. };
  143. /* FM NA 2.0 settings */
  144. static const struct max2175_reg_map fmna2p0_map[] = {
  145. { 0x01, 0x13 }, { 0x02, 0x08 }, { 0x03, 0x8d }, { 0x04, 0xc0 },
  146. { 0x05, 0x35 }, { 0x06, 0x18 }, { 0x07, 0x7c }, { 0x08, 0x54 },
  147. { 0x09, 0xa7 }, { 0x0a, 0x55 }, { 0x0b, 0x42 }, { 0x0c, 0x48 },
  148. { 0x0e, 0x7a }, { 0x0f, 0x88 }, { 0x10, 0x91 }, { 0x11, 0x61 },
  149. { 0x12, 0x61 }, { 0x13, 0x61 }, { 0x14, 0x61 }, { 0x15, 0x5c },
  150. { 0x16, 0x0f }, { 0x17, 0x34 }, { 0x18, 0x1c }, { 0x1a, 0x88 },
  151. { 0x1b, 0x33 }, { 0x1c, 0x02 }, { 0x1d, 0x00 }, { 0x1e, 0x01 },
  152. { 0x23, 0x80 }, { 0x24, 0x00 }, { 0x25, 0x95 }, { 0x26, 0x05 },
  153. { 0x27, 0x2c }, { 0x32, 0x08 }, { 0x33, 0xa8 }, { 0x36, 0xaf },
  154. { 0x37, 0x7e }, { 0x55, 0xbf }, { 0x56, 0x3f }, { 0x57, 0xff },
  155. { 0x58, 0x9f }, { 0x76, 0xac }, { 0x77, 0xc0 }, { 0x78, 0x00 },
  156. { 0x79, 0x00 }, { 0x82, 0x6b }, { 0x83, 0x00 }, { 0x85, 0x11 },
  157. { 0x86, 0x3f },
  158. };
  159. static const u16 ch_coeff_dab1[] = {
  160. 0x001c, 0x0007, 0xffcd, 0x0056, 0xffa4, 0x0033, 0x0027, 0xff61,
  161. 0x010e, 0xfec0, 0x0106, 0xffb8, 0xff1c, 0x023c, 0xfcb2, 0x039b,
  162. 0xfd4e, 0x0055, 0x036a, 0xf7de, 0x0d21, 0xee72, 0x1499, 0x6a51,
  163. };
  164. static const u16 ch_coeff_fmeu[] = {
  165. 0x0000, 0xffff, 0x0001, 0x0002, 0xfffa, 0xffff, 0x0015, 0xffec,
  166. 0xffde, 0x0054, 0xfff9, 0xff52, 0x00b8, 0x00a2, 0xfe0a, 0x00af,
  167. 0x02e3, 0xfc14, 0xfe89, 0x089d, 0xfa2e, 0xf30f, 0x25be, 0x4eb6,
  168. };
  169. static const u16 eq_coeff_fmeu1_ra02_m6db[] = {
  170. 0x0040, 0xffc6, 0xfffa, 0x002c, 0x000d, 0xff90, 0x0037, 0x006e,
  171. 0xffc0, 0xff5b, 0x006a, 0x00f0, 0xff57, 0xfe94, 0x0112, 0x0252,
  172. 0xfe0c, 0xfc6a, 0x0385, 0x0553, 0xfa49, 0xf789, 0x0b91, 0x1a10,
  173. };
  174. static const u16 ch_coeff_fmna[] = {
  175. 0x0001, 0x0003, 0xfffe, 0xfff4, 0x0000, 0x001f, 0x000c, 0xffbc,
  176. 0xffd3, 0x007d, 0x0075, 0xff33, 0xff01, 0x0131, 0x01ef, 0xfe60,
  177. 0xfc7a, 0x020e, 0x0656, 0xfd94, 0xf395, 0x02ab, 0x2857, 0x3d3f,
  178. };
  179. static const u16 eq_coeff_fmna1_ra02_m6db[] = {
  180. 0xfff1, 0xffe1, 0xffef, 0x000e, 0x0030, 0x002f, 0xfff6, 0xffa7,
  181. 0xff9d, 0x000a, 0x00a2, 0x00b5, 0xffea, 0xfed9, 0xfec5, 0x003d,
  182. 0x0217, 0x021b, 0xff5a, 0xfc2b, 0xfcbd, 0x02c4, 0x0ac3, 0x0e85,
  183. };
  184. static const u8 adc_presets[2][23] = {
  185. {
  186. 0x83, 0x00, 0xcf, 0xb4, 0x0f, 0x2c, 0x0c, 0x49,
  187. 0x00, 0x00, 0x00, 0x8c, 0x02, 0x02, 0x00, 0x04,
  188. 0xec, 0x82, 0x4b, 0xcc, 0x01, 0x88, 0x0c,
  189. },
  190. {
  191. 0x83, 0x00, 0xcf, 0xb4, 0x0f, 0x2c, 0x0c, 0x49,
  192. 0x00, 0x00, 0x00, 0x8c, 0x02, 0x20, 0x33, 0x8c,
  193. 0x57, 0xd7, 0x59, 0xb7, 0x65, 0x0e, 0x0c,
  194. },
  195. };
  196. /* Tuner bands */
  197. static const struct v4l2_frequency_band eu_bands_rf = {
  198. .tuner = 0,
  199. .type = V4L2_TUNER_RF,
  200. .index = 0,
  201. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  202. .rangelow = 65000000,
  203. .rangehigh = 240000000,
  204. };
  205. static const struct v4l2_frequency_band na_bands_rf = {
  206. .tuner = 0,
  207. .type = V4L2_TUNER_RF,
  208. .index = 0,
  209. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  210. .rangelow = 65000000,
  211. .rangehigh = 108000000,
  212. };
  213. /* Regmap settings */
  214. static const struct regmap_range max2175_regmap_volatile_range[] = {
  215. regmap_reg_range(0x30, 0x35),
  216. regmap_reg_range(0x3a, 0x45),
  217. regmap_reg_range(0x59, 0x5e),
  218. regmap_reg_range(0x73, 0x75),
  219. };
  220. static const struct regmap_access_table max2175_volatile_regs = {
  221. .yes_ranges = max2175_regmap_volatile_range,
  222. .n_yes_ranges = ARRAY_SIZE(max2175_regmap_volatile_range),
  223. };
  224. static const struct reg_default max2175_reg_defaults[] = {
  225. { 0x00, 0x07},
  226. };
  227. static const struct regmap_config max2175_regmap_config = {
  228. .reg_bits = 8,
  229. .val_bits = 8,
  230. .max_register = 0xff,
  231. .reg_defaults = max2175_reg_defaults,
  232. .num_reg_defaults = ARRAY_SIZE(max2175_reg_defaults),
  233. .volatile_table = &max2175_volatile_regs,
  234. .cache_type = REGCACHE_MAPLE,
  235. };
  236. struct max2175 {
  237. struct v4l2_subdev sd; /* Sub-device */
  238. struct i2c_client *client; /* I2C client */
  239. /* Controls */
  240. struct v4l2_ctrl_handler ctrl_hdl;
  241. struct v4l2_ctrl *lna_gain; /* LNA gain value */
  242. struct v4l2_ctrl *if_gain; /* I/F gain value */
  243. struct v4l2_ctrl *pll_lock; /* PLL lock */
  244. struct v4l2_ctrl *i2s_en; /* I2S output enable */
  245. struct v4l2_ctrl *hsls; /* High-side/Low-side polarity */
  246. struct v4l2_ctrl *rx_mode; /* Receive mode */
  247. /* Regmap */
  248. struct regmap *regmap;
  249. /* Cached configuration */
  250. u32 freq; /* Tuned freq In Hz */
  251. const struct max2175_rxmode *rx_modes; /* EU or NA modes */
  252. const struct v4l2_frequency_band *bands_rf; /* EU or NA bands */
  253. /* Device settings */
  254. unsigned long xtal_freq; /* Ref Oscillator freq in Hz */
  255. u32 decim_ratio;
  256. bool master; /* Master/Slave */
  257. bool am_hiz; /* AM Hi-Z filter */
  258. /* ROM values */
  259. u8 rom_bbf_bw_am;
  260. u8 rom_bbf_bw_fm;
  261. u8 rom_bbf_bw_dab;
  262. /* Driver private variables */
  263. bool mode_resolved; /* Flag to sanity check settings */
  264. };
  265. static inline struct max2175 *max2175_from_sd(struct v4l2_subdev *sd)
  266. {
  267. return container_of(sd, struct max2175, sd);
  268. }
  269. static inline struct max2175 *max2175_from_ctrl_hdl(struct v4l2_ctrl_handler *h)
  270. {
  271. return container_of(h, struct max2175, ctrl_hdl);
  272. }
  273. /* Get bitval of a given val */
  274. static inline u8 max2175_get_bitval(u8 val, u8 msb, u8 lsb)
  275. {
  276. return (val & GENMASK(msb, lsb)) >> lsb;
  277. }
  278. /* Read/Write bit(s) on top of regmap */
  279. static int max2175_read(struct max2175 *ctx, u8 idx, u8 *val)
  280. {
  281. u32 regval;
  282. int ret;
  283. ret = regmap_read(ctx->regmap, idx, &regval);
  284. if (ret)
  285. mxm_err(ctx, "read ret(%d): idx 0x%02x\n", ret, idx);
  286. else
  287. *val = regval;
  288. return ret;
  289. }
  290. static int max2175_write(struct max2175 *ctx, u8 idx, u8 val)
  291. {
  292. int ret;
  293. ret = regmap_write(ctx->regmap, idx, val);
  294. if (ret)
  295. mxm_err(ctx, "write ret(%d): idx 0x%02x val 0x%02x\n",
  296. ret, idx, val);
  297. return ret;
  298. }
  299. static u8 max2175_read_bits(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb)
  300. {
  301. u8 val;
  302. if (max2175_read(ctx, idx, &val))
  303. return 0;
  304. return max2175_get_bitval(val, msb, lsb);
  305. }
  306. static int max2175_write_bits(struct max2175 *ctx, u8 idx,
  307. u8 msb, u8 lsb, u8 newval)
  308. {
  309. int ret = regmap_update_bits(ctx->regmap, idx, GENMASK(msb, lsb),
  310. newval << lsb);
  311. if (ret)
  312. mxm_err(ctx, "wbits ret(%d): idx 0x%02x\n", ret, idx);
  313. return ret;
  314. }
  315. static int max2175_write_bit(struct max2175 *ctx, u8 idx, u8 bit, u8 newval)
  316. {
  317. return max2175_write_bits(ctx, idx, bit, bit, newval);
  318. }
  319. /* Checks expected pattern every msec until timeout */
  320. static int max2175_poll_timeout(struct max2175 *ctx, u8 idx, u8 msb, u8 lsb,
  321. u8 exp_bitval, u32 timeout_us)
  322. {
  323. unsigned int val;
  324. return regmap_read_poll_timeout(ctx->regmap, idx, val,
  325. (max2175_get_bitval(val, msb, lsb) == exp_bitval),
  326. 1000, timeout_us);
  327. }
  328. static int max2175_poll_csm_ready(struct max2175 *ctx)
  329. {
  330. int ret;
  331. ret = max2175_poll_timeout(ctx, 69, 1, 1, 0, 50000);
  332. if (ret)
  333. mxm_err(ctx, "csm not ready\n");
  334. return ret;
  335. }
  336. #define MAX2175_IS_BAND_AM(ctx) \
  337. (max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_AM)
  338. #define MAX2175_IS_BAND_VHF(ctx) \
  339. (max2175_read_bits(ctx, 5, 1, 0) == MAX2175_BAND_VHF)
  340. #define MAX2175_IS_FM_MODE(ctx) \
  341. (max2175_read_bits(ctx, 12, 5, 4) == 0)
  342. #define MAX2175_IS_FMHD_MODE(ctx) \
  343. (max2175_read_bits(ctx, 12, 5, 4) == 1)
  344. #define MAX2175_IS_DAB_MODE(ctx) \
  345. (max2175_read_bits(ctx, 12, 5, 4) == 2)
  346. static int max2175_band_from_freq(u32 freq)
  347. {
  348. if (freq >= 144000 && freq <= 26100000)
  349. return MAX2175_BAND_AM;
  350. else if (freq >= 65000000 && freq <= 108000000)
  351. return MAX2175_BAND_FM;
  352. return MAX2175_BAND_VHF;
  353. }
  354. static void max2175_i2s_enable(struct max2175 *ctx, bool enable)
  355. {
  356. if (enable)
  357. /* Stuff bits are zeroed */
  358. max2175_write_bits(ctx, 104, 3, 0, 2);
  359. else
  360. /* Keep SCK alive */
  361. max2175_write_bits(ctx, 104, 3, 0, 9);
  362. mxm_dbg(ctx, "i2s %sabled\n", enable ? "en" : "dis");
  363. }
  364. static void max2175_set_filter_coeffs(struct max2175 *ctx, u8 m_sel,
  365. u8 bank, const u16 *coeffs)
  366. {
  367. unsigned int i;
  368. u8 coeff_addr, upper_address = 24;
  369. mxm_dbg(ctx, "set_filter_coeffs: m_sel %d bank %d\n", m_sel, bank);
  370. max2175_write_bits(ctx, 114, 5, 4, m_sel);
  371. if (m_sel == 2)
  372. upper_address = 12;
  373. for (i = 0; i < upper_address; i++) {
  374. coeff_addr = i + bank * 24;
  375. max2175_write(ctx, 115, coeffs[i] >> 8);
  376. max2175_write(ctx, 116, coeffs[i]);
  377. max2175_write(ctx, 117, coeff_addr | 1 << 7);
  378. }
  379. max2175_write_bit(ctx, 117, 7, 0);
  380. }
  381. static void max2175_load_fmeu_1p2(struct max2175 *ctx)
  382. {
  383. unsigned int i;
  384. for (i = 0; i < ARRAY_SIZE(fmeu1p2_map); i++)
  385. max2175_write(ctx, fmeu1p2_map[i].idx, fmeu1p2_map[i].val);
  386. ctx->decim_ratio = 36;
  387. /* Load the Channel Filter Coefficients into channel filter bank #2 */
  388. max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmeu);
  389. max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
  390. eq_coeff_fmeu1_ra02_m6db);
  391. }
  392. static void max2175_load_dab_1p2(struct max2175 *ctx)
  393. {
  394. unsigned int i;
  395. for (i = 0; i < ARRAY_SIZE(dab12_map); i++)
  396. max2175_write(ctx, dab12_map[i].idx, dab12_map[i].val);
  397. ctx->decim_ratio = 1;
  398. /* Load the Channel Filter Coefficients into channel filter bank #2 */
  399. max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 2, ch_coeff_dab1);
  400. }
  401. static void max2175_load_fmna_1p0(struct max2175 *ctx)
  402. {
  403. unsigned int i;
  404. for (i = 0; i < ARRAY_SIZE(fmna1p0_map); i++)
  405. max2175_write(ctx, fmna1p0_map[i].idx, fmna1p0_map[i].val);
  406. }
  407. static void max2175_load_fmna_2p0(struct max2175 *ctx)
  408. {
  409. unsigned int i;
  410. for (i = 0; i < ARRAY_SIZE(fmna2p0_map); i++)
  411. max2175_write(ctx, fmna2p0_map[i].idx, fmna2p0_map[i].val);
  412. }
  413. static void max2175_set_bbfilter(struct max2175 *ctx)
  414. {
  415. if (MAX2175_IS_BAND_AM(ctx)) {
  416. max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_am);
  417. mxm_dbg(ctx, "set_bbfilter AM: rom %d\n", ctx->rom_bbf_bw_am);
  418. } else if (MAX2175_IS_DAB_MODE(ctx)) {
  419. max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_dab);
  420. mxm_dbg(ctx, "set_bbfilter DAB: rom %d\n", ctx->rom_bbf_bw_dab);
  421. } else {
  422. max2175_write_bits(ctx, 12, 3, 0, ctx->rom_bbf_bw_fm);
  423. mxm_dbg(ctx, "set_bbfilter FM: rom %d\n", ctx->rom_bbf_bw_fm);
  424. }
  425. }
  426. static int max2175_set_csm_mode(struct max2175 *ctx,
  427. enum max2175_csm_mode new_mode)
  428. {
  429. int ret = max2175_poll_csm_ready(ctx);
  430. if (ret)
  431. return ret;
  432. max2175_write_bits(ctx, 0, 2, 0, new_mode);
  433. mxm_dbg(ctx, "set csm new mode %d\n", new_mode);
  434. /* Wait for a fixed settle down time depending on new mode */
  435. switch (new_mode) {
  436. case MAX2175_PRESET_TUNE:
  437. usleep_range(51100, 51500); /* 51.1ms */
  438. break;
  439. /*
  440. * Other mode switches need different sleep values depending on band &
  441. * mode
  442. */
  443. default:
  444. break;
  445. }
  446. return max2175_poll_csm_ready(ctx);
  447. }
  448. static int max2175_csm_action(struct max2175 *ctx,
  449. enum max2175_csm_mode action)
  450. {
  451. int ret;
  452. mxm_dbg(ctx, "csm_action: %d\n", action);
  453. /* Other actions can be added in future when needed */
  454. ret = max2175_set_csm_mode(ctx, MAX2175_LOAD_TO_BUFFER);
  455. if (ret)
  456. return ret;
  457. return max2175_set_csm_mode(ctx, MAX2175_PRESET_TUNE);
  458. }
  459. static int max2175_set_lo_freq(struct max2175 *ctx, u32 lo_freq)
  460. {
  461. u8 lo_mult, loband_bits = 0, vcodiv_bits = 0;
  462. u32 int_desired, frac_desired;
  463. enum max2175_band band;
  464. int ret;
  465. band = max2175_read_bits(ctx, 5, 1, 0);
  466. switch (band) {
  467. case MAX2175_BAND_AM:
  468. lo_mult = 16;
  469. break;
  470. case MAX2175_BAND_FM:
  471. if (lo_freq <= 74700000) {
  472. lo_mult = 16;
  473. } else if (lo_freq > 74700000 && lo_freq <= 110000000) {
  474. loband_bits = 1;
  475. lo_mult = 8;
  476. } else {
  477. loband_bits = 1;
  478. vcodiv_bits = 3;
  479. lo_mult = 8;
  480. }
  481. break;
  482. case MAX2175_BAND_VHF:
  483. if (lo_freq <= 210000000)
  484. vcodiv_bits = 2;
  485. else
  486. vcodiv_bits = 1;
  487. loband_bits = 2;
  488. lo_mult = 4;
  489. break;
  490. default:
  491. loband_bits = 3;
  492. vcodiv_bits = 2;
  493. lo_mult = 2;
  494. break;
  495. }
  496. if (band == MAX2175_BAND_L)
  497. lo_freq /= lo_mult;
  498. else
  499. lo_freq *= lo_mult;
  500. int_desired = lo_freq / ctx->xtal_freq;
  501. frac_desired = div64_ul((u64)(lo_freq % ctx->xtal_freq) << 20,
  502. ctx->xtal_freq);
  503. /* Check CSM is not busy */
  504. ret = max2175_poll_csm_ready(ctx);
  505. if (ret)
  506. return ret;
  507. mxm_dbg(ctx, "lo_mult %u int %u frac %u\n",
  508. lo_mult, int_desired, frac_desired);
  509. /* Write the calculated values to the appropriate registers */
  510. max2175_write(ctx, 1, int_desired);
  511. max2175_write_bits(ctx, 2, 3, 0, (frac_desired >> 16) & 0xf);
  512. max2175_write(ctx, 3, frac_desired >> 8);
  513. max2175_write(ctx, 4, frac_desired);
  514. max2175_write_bits(ctx, 5, 3, 2, loband_bits);
  515. max2175_write_bits(ctx, 6, 7, 6, vcodiv_bits);
  516. return ret;
  517. }
  518. /*
  519. * Helper similar to DIV_ROUND_CLOSEST but an inline function that accepts s64
  520. * dividend and s32 divisor
  521. */
  522. static inline s64 max2175_round_closest(s64 dividend, s32 divisor)
  523. {
  524. if ((dividend > 0 && divisor > 0) || (dividend < 0 && divisor < 0))
  525. return div_s64(dividend + divisor / 2, divisor);
  526. return div_s64(dividend - divisor / 2, divisor);
  527. }
  528. static int max2175_set_nco_freq(struct max2175 *ctx, s32 nco_freq)
  529. {
  530. s32 clock_rate = ctx->xtal_freq / ctx->decim_ratio;
  531. u32 nco_reg, abs_nco_freq = abs(nco_freq);
  532. s64 nco_val_desired;
  533. int ret;
  534. if (abs_nco_freq < clock_rate / 2) {
  535. nco_val_desired = 2 * nco_freq;
  536. } else {
  537. nco_val_desired = 2LL * (clock_rate - abs_nco_freq);
  538. if (nco_freq < 0)
  539. nco_val_desired = -nco_val_desired;
  540. }
  541. nco_reg = max2175_round_closest(nco_val_desired << 20, clock_rate);
  542. if (nco_freq < 0)
  543. nco_reg += 0x200000;
  544. /* Check CSM is not busy */
  545. ret = max2175_poll_csm_ready(ctx);
  546. if (ret)
  547. return ret;
  548. mxm_dbg(ctx, "freq %d desired %lld reg %u\n",
  549. nco_freq, nco_val_desired, nco_reg);
  550. /* Write the calculated values to the appropriate registers */
  551. max2175_write_bits(ctx, 7, 4, 0, (nco_reg >> 16) & 0x1f);
  552. max2175_write(ctx, 8, nco_reg >> 8);
  553. max2175_write(ctx, 9, nco_reg);
  554. return ret;
  555. }
  556. static int max2175_set_rf_freq_non_am_bands(struct max2175 *ctx, u64 freq,
  557. u32 lo_pos)
  558. {
  559. s64 adj_freq, low_if_freq;
  560. int ret;
  561. mxm_dbg(ctx, "rf_freq: non AM bands\n");
  562. if (MAX2175_IS_FM_MODE(ctx))
  563. low_if_freq = 128000;
  564. else if (MAX2175_IS_FMHD_MODE(ctx))
  565. low_if_freq = 228000;
  566. else
  567. return max2175_set_lo_freq(ctx, freq);
  568. if (MAX2175_IS_BAND_VHF(ctx) == (lo_pos == MAX2175_LO_ABOVE_DESIRED))
  569. adj_freq = freq + low_if_freq;
  570. else
  571. adj_freq = freq - low_if_freq;
  572. ret = max2175_set_lo_freq(ctx, adj_freq);
  573. if (ret)
  574. return ret;
  575. return max2175_set_nco_freq(ctx, -low_if_freq);
  576. }
  577. static int max2175_set_rf_freq(struct max2175 *ctx, u64 freq, u32 lo_pos)
  578. {
  579. int ret;
  580. if (MAX2175_IS_BAND_AM(ctx))
  581. ret = max2175_set_nco_freq(ctx, freq);
  582. else
  583. ret = max2175_set_rf_freq_non_am_bands(ctx, freq, lo_pos);
  584. mxm_dbg(ctx, "set_rf_freq: ret %d freq %llu\n", ret, freq);
  585. return ret;
  586. }
  587. static int max2175_tune_rf_freq(struct max2175 *ctx, u64 freq, u32 hsls)
  588. {
  589. int ret;
  590. ret = max2175_set_rf_freq(ctx, freq, hsls);
  591. if (ret)
  592. return ret;
  593. ret = max2175_csm_action(ctx, MAX2175_BUFFER_PLUS_PRESET_TUNE);
  594. if (ret)
  595. return ret;
  596. mxm_dbg(ctx, "tune_rf_freq: old %u new %llu\n", ctx->freq, freq);
  597. ctx->freq = freq;
  598. return ret;
  599. }
  600. static void max2175_set_hsls(struct max2175 *ctx, u32 lo_pos)
  601. {
  602. mxm_dbg(ctx, "set_hsls: lo_pos %u\n", lo_pos);
  603. if ((lo_pos == MAX2175_LO_BELOW_DESIRED) == MAX2175_IS_BAND_VHF(ctx))
  604. max2175_write_bit(ctx, 5, 4, 1);
  605. else
  606. max2175_write_bit(ctx, 5, 4, 0);
  607. }
  608. static void max2175_set_eu_rx_mode(struct max2175 *ctx, u32 rx_mode)
  609. {
  610. switch (rx_mode) {
  611. case MAX2175_EU_FM_1_2:
  612. max2175_load_fmeu_1p2(ctx);
  613. break;
  614. case MAX2175_DAB_1_2:
  615. max2175_load_dab_1p2(ctx);
  616. break;
  617. }
  618. /* Master is the default setting */
  619. if (!ctx->master)
  620. max2175_write_bit(ctx, 30, 7, 1);
  621. }
  622. static void max2175_set_na_rx_mode(struct max2175 *ctx, u32 rx_mode)
  623. {
  624. switch (rx_mode) {
  625. case MAX2175_NA_FM_1_0:
  626. max2175_load_fmna_1p0(ctx);
  627. break;
  628. case MAX2175_NA_FM_2_0:
  629. max2175_load_fmna_2p0(ctx);
  630. break;
  631. }
  632. /* Master is the default setting */
  633. if (!ctx->master)
  634. max2175_write_bit(ctx, 30, 7, 1);
  635. ctx->decim_ratio = 27;
  636. /* Load the Channel Filter Coefficients into channel filter bank #2 */
  637. max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0, ch_coeff_fmna);
  638. max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
  639. eq_coeff_fmna1_ra02_m6db);
  640. }
  641. static int max2175_set_rx_mode(struct max2175 *ctx, u32 rx_mode)
  642. {
  643. mxm_dbg(ctx, "set_rx_mode: %u am_hiz %u\n", rx_mode, ctx->am_hiz);
  644. if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
  645. max2175_set_eu_rx_mode(ctx, rx_mode);
  646. else
  647. max2175_set_na_rx_mode(ctx, rx_mode);
  648. if (ctx->am_hiz) {
  649. mxm_dbg(ctx, "setting AM HiZ related config\n");
  650. max2175_write_bit(ctx, 50, 5, 1);
  651. max2175_write_bit(ctx, 90, 7, 1);
  652. max2175_write_bits(ctx, 73, 1, 0, 2);
  653. max2175_write_bits(ctx, 80, 5, 0, 33);
  654. }
  655. /* Load BB filter trim values saved in ROM */
  656. max2175_set_bbfilter(ctx);
  657. /* Set HSLS */
  658. max2175_set_hsls(ctx, ctx->hsls->cur.val);
  659. /* Use i2s enable settings */
  660. max2175_i2s_enable(ctx, ctx->i2s_en->cur.val);
  661. ctx->mode_resolved = true;
  662. return 0;
  663. }
  664. static int max2175_rx_mode_from_freq(struct max2175 *ctx, u32 freq, u32 *mode)
  665. {
  666. unsigned int i;
  667. int band = max2175_band_from_freq(freq);
  668. /* Pick the first match always */
  669. for (i = 0; i <= ctx->rx_mode->maximum; i++) {
  670. if (ctx->rx_modes[i].band == band) {
  671. *mode = i;
  672. mxm_dbg(ctx, "rx_mode_from_freq: freq %u mode %d\n",
  673. freq, *mode);
  674. return 0;
  675. }
  676. }
  677. return -EINVAL;
  678. }
  679. static bool max2175_freq_rx_mode_valid(struct max2175 *ctx,
  680. u32 mode, u32 freq)
  681. {
  682. int band = max2175_band_from_freq(freq);
  683. return (ctx->rx_modes[mode].band == band);
  684. }
  685. static void max2175_load_adc_presets(struct max2175 *ctx)
  686. {
  687. unsigned int i, j;
  688. for (i = 0; i < ARRAY_SIZE(adc_presets); i++)
  689. for (j = 0; j < ARRAY_SIZE(adc_presets[0]); j++)
  690. max2175_write(ctx, 146 + j + i * 55, adc_presets[i][j]);
  691. }
  692. static int max2175_init_power_manager(struct max2175 *ctx)
  693. {
  694. int ret;
  695. /* Execute on-chip power-up/calibration */
  696. max2175_write_bit(ctx, 99, 2, 0);
  697. usleep_range(1000, 1500);
  698. max2175_write_bit(ctx, 99, 2, 1);
  699. /* Wait for the power manager to finish. */
  700. ret = max2175_poll_timeout(ctx, 69, 7, 7, 1, 50000);
  701. if (ret)
  702. mxm_err(ctx, "init pm failed\n");
  703. return ret;
  704. }
  705. static int max2175_recalibrate_adc(struct max2175 *ctx)
  706. {
  707. int ret;
  708. /* ADC Re-calibration */
  709. max2175_write(ctx, 150, 0xff);
  710. max2175_write(ctx, 205, 0xff);
  711. max2175_write(ctx, 147, 0x20);
  712. max2175_write(ctx, 147, 0x00);
  713. max2175_write(ctx, 202, 0x20);
  714. max2175_write(ctx, 202, 0x00);
  715. ret = max2175_poll_timeout(ctx, 69, 4, 3, 3, 50000);
  716. if (ret)
  717. mxm_err(ctx, "adc recalibration failed\n");
  718. return ret;
  719. }
  720. static u8 max2175_read_rom(struct max2175 *ctx, u8 row)
  721. {
  722. u8 data = 0;
  723. max2175_write_bit(ctx, 56, 4, 0);
  724. max2175_write_bits(ctx, 56, 3, 0, row);
  725. usleep_range(2000, 2500);
  726. max2175_read(ctx, 58, &data);
  727. max2175_write_bits(ctx, 56, 3, 0, 0);
  728. mxm_dbg(ctx, "read_rom: row %d data 0x%02x\n", row, data);
  729. return data;
  730. }
  731. static void max2175_load_from_rom(struct max2175 *ctx)
  732. {
  733. u8 data = 0;
  734. data = max2175_read_rom(ctx, 0);
  735. ctx->rom_bbf_bw_am = data & 0x0f;
  736. max2175_write_bits(ctx, 81, 3, 0, data >> 4);
  737. data = max2175_read_rom(ctx, 1);
  738. ctx->rom_bbf_bw_fm = data & 0x0f;
  739. ctx->rom_bbf_bw_dab = data >> 4;
  740. data = max2175_read_rom(ctx, 2);
  741. max2175_write_bits(ctx, 82, 4, 0, data & 0x1f);
  742. max2175_write_bits(ctx, 82, 7, 5, data >> 5);
  743. data = max2175_read_rom(ctx, 3);
  744. if (ctx->am_hiz) {
  745. data &= 0x0f;
  746. data |= (max2175_read_rom(ctx, 7) & 0x40) >> 2;
  747. if (!data)
  748. data |= 2;
  749. } else {
  750. data = (data & 0xf0) >> 4;
  751. data |= (max2175_read_rom(ctx, 7) & 0x80) >> 3;
  752. if (!data)
  753. data |= 30;
  754. }
  755. max2175_write_bits(ctx, 80, 5, 0, data + 31);
  756. data = max2175_read_rom(ctx, 6);
  757. max2175_write_bits(ctx, 81, 7, 6, data >> 6);
  758. }
  759. static void max2175_load_full_fm_eu_1p0(struct max2175 *ctx)
  760. {
  761. unsigned int i;
  762. for (i = 0; i < ARRAY_SIZE(full_fm_eu_1p0); i++)
  763. max2175_write(ctx, i + 1, full_fm_eu_1p0[i]);
  764. usleep_range(5000, 5500);
  765. ctx->decim_ratio = 36;
  766. }
  767. static void max2175_load_full_fm_na_1p0(struct max2175 *ctx)
  768. {
  769. unsigned int i;
  770. for (i = 0; i < ARRAY_SIZE(full_fm_na_1p0); i++)
  771. max2175_write(ctx, i + 1, full_fm_na_1p0[i]);
  772. usleep_range(5000, 5500);
  773. ctx->decim_ratio = 27;
  774. }
  775. static int max2175_core_init(struct max2175 *ctx, u32 refout_bits)
  776. {
  777. int ret;
  778. /* MAX2175 uses 36.864MHz clock for EU & 40.154MHz for NA region */
  779. if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ)
  780. max2175_load_full_fm_eu_1p0(ctx);
  781. else
  782. max2175_load_full_fm_na_1p0(ctx);
  783. /* The default settings assume master */
  784. if (!ctx->master)
  785. max2175_write_bit(ctx, 30, 7, 1);
  786. mxm_dbg(ctx, "refout_bits %u\n", refout_bits);
  787. /* Set REFOUT */
  788. max2175_write_bits(ctx, 56, 7, 5, refout_bits);
  789. /* ADC Reset */
  790. max2175_write_bit(ctx, 99, 1, 0);
  791. usleep_range(1000, 1500);
  792. max2175_write_bit(ctx, 99, 1, 1);
  793. /* Load ADC preset values */
  794. max2175_load_adc_presets(ctx);
  795. /* Initialize the power management state machine */
  796. ret = max2175_init_power_manager(ctx);
  797. if (ret)
  798. return ret;
  799. /* Recalibrate ADC */
  800. ret = max2175_recalibrate_adc(ctx);
  801. if (ret)
  802. return ret;
  803. /* Load ROM values to appropriate registers */
  804. max2175_load_from_rom(ctx);
  805. if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
  806. /* Load FIR coefficients into bank 0 */
  807. max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
  808. ch_coeff_fmeu);
  809. max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
  810. eq_coeff_fmeu1_ra02_m6db);
  811. } else {
  812. /* Load FIR coefficients into bank 0 */
  813. max2175_set_filter_coeffs(ctx, MAX2175_CH_MSEL, 0,
  814. ch_coeff_fmna);
  815. max2175_set_filter_coeffs(ctx, MAX2175_EQ_MSEL, 0,
  816. eq_coeff_fmna1_ra02_m6db);
  817. }
  818. mxm_dbg(ctx, "core initialized\n");
  819. return 0;
  820. }
  821. static void max2175_s_ctrl_rx_mode(struct max2175 *ctx, u32 rx_mode)
  822. {
  823. /* Load mode. Range check already done */
  824. max2175_set_rx_mode(ctx, rx_mode);
  825. mxm_dbg(ctx, "s_ctrl_rx_mode: %u curr freq %u\n", rx_mode, ctx->freq);
  826. /* Check if current freq valid for mode & update */
  827. if (max2175_freq_rx_mode_valid(ctx, rx_mode, ctx->freq))
  828. max2175_tune_rf_freq(ctx, ctx->freq, ctx->hsls->cur.val);
  829. else
  830. /* Use default freq of mode if current freq is not valid */
  831. max2175_tune_rf_freq(ctx, ctx->rx_modes[rx_mode].freq,
  832. ctx->hsls->cur.val);
  833. }
  834. static int max2175_s_ctrl(struct v4l2_ctrl *ctrl)
  835. {
  836. struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
  837. mxm_dbg(ctx, "s_ctrl: id 0x%x, val %u\n", ctrl->id, ctrl->val);
  838. switch (ctrl->id) {
  839. case V4L2_CID_MAX2175_I2S_ENABLE:
  840. max2175_i2s_enable(ctx, ctrl->val);
  841. break;
  842. case V4L2_CID_MAX2175_HSLS:
  843. max2175_set_hsls(ctx, ctrl->val);
  844. break;
  845. case V4L2_CID_MAX2175_RX_MODE:
  846. max2175_s_ctrl_rx_mode(ctx, ctrl->val);
  847. break;
  848. }
  849. return 0;
  850. }
  851. static u32 max2175_get_lna_gain(struct max2175 *ctx)
  852. {
  853. enum max2175_band band = max2175_read_bits(ctx, 5, 1, 0);
  854. switch (band) {
  855. case MAX2175_BAND_AM:
  856. return max2175_read_bits(ctx, 51, 3, 0);
  857. case MAX2175_BAND_FM:
  858. return max2175_read_bits(ctx, 50, 3, 0);
  859. case MAX2175_BAND_VHF:
  860. return max2175_read_bits(ctx, 52, 5, 0);
  861. default:
  862. return 0;
  863. }
  864. }
  865. static int max2175_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  866. {
  867. struct max2175 *ctx = max2175_from_ctrl_hdl(ctrl->handler);
  868. switch (ctrl->id) {
  869. case V4L2_CID_RF_TUNER_LNA_GAIN:
  870. ctrl->val = max2175_get_lna_gain(ctx);
  871. break;
  872. case V4L2_CID_RF_TUNER_IF_GAIN:
  873. ctrl->val = max2175_read_bits(ctx, 49, 4, 0);
  874. break;
  875. case V4L2_CID_RF_TUNER_PLL_LOCK:
  876. ctrl->val = (max2175_read_bits(ctx, 60, 7, 6) == 3);
  877. break;
  878. }
  879. return 0;
  880. };
  881. static int max2175_set_freq_and_mode(struct max2175 *ctx, u32 freq)
  882. {
  883. u32 rx_mode;
  884. int ret;
  885. /* Get band from frequency */
  886. ret = max2175_rx_mode_from_freq(ctx, freq, &rx_mode);
  887. if (ret)
  888. return ret;
  889. mxm_dbg(ctx, "set_freq_and_mode: freq %u rx_mode %d\n", freq, rx_mode);
  890. /* Load mode */
  891. max2175_set_rx_mode(ctx, rx_mode);
  892. ctx->rx_mode->cur.val = rx_mode;
  893. /* Tune to the new freq given */
  894. return max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
  895. }
  896. static int max2175_s_frequency(struct v4l2_subdev *sd,
  897. const struct v4l2_frequency *vf)
  898. {
  899. struct max2175 *ctx = max2175_from_sd(sd);
  900. u32 freq;
  901. int ret = 0;
  902. mxm_dbg(ctx, "s_freq: new %u curr %u, mode_resolved %d\n",
  903. vf->frequency, ctx->freq, ctx->mode_resolved);
  904. if (vf->tuner != 0)
  905. return -EINVAL;
  906. freq = clamp(vf->frequency, ctx->bands_rf->rangelow,
  907. ctx->bands_rf->rangehigh);
  908. /* Check new freq valid for rx_mode if already resolved */
  909. if (ctx->mode_resolved &&
  910. max2175_freq_rx_mode_valid(ctx, ctx->rx_mode->cur.val, freq))
  911. ret = max2175_tune_rf_freq(ctx, freq, ctx->hsls->cur.val);
  912. else
  913. /* Find default rx_mode for freq and tune to it */
  914. ret = max2175_set_freq_and_mode(ctx, freq);
  915. mxm_dbg(ctx, "s_freq: ret %d curr %u mode_resolved %d mode %u\n",
  916. ret, ctx->freq, ctx->mode_resolved, ctx->rx_mode->cur.val);
  917. return ret;
  918. }
  919. static int max2175_g_frequency(struct v4l2_subdev *sd,
  920. struct v4l2_frequency *vf)
  921. {
  922. struct max2175 *ctx = max2175_from_sd(sd);
  923. if (vf->tuner != 0)
  924. return -EINVAL;
  925. /* RF freq */
  926. vf->type = V4L2_TUNER_RF;
  927. vf->frequency = ctx->freq;
  928. return 0;
  929. }
  930. static int max2175_enum_freq_bands(struct v4l2_subdev *sd,
  931. struct v4l2_frequency_band *band)
  932. {
  933. struct max2175 *ctx = max2175_from_sd(sd);
  934. if (band->tuner != 0 || band->index != 0)
  935. return -EINVAL;
  936. *band = *ctx->bands_rf;
  937. return 0;
  938. }
  939. static int max2175_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  940. {
  941. struct max2175 *ctx = max2175_from_sd(sd);
  942. if (vt->index > 0)
  943. return -EINVAL;
  944. strscpy(vt->name, "RF", sizeof(vt->name));
  945. vt->type = V4L2_TUNER_RF;
  946. vt->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  947. vt->rangelow = ctx->bands_rf->rangelow;
  948. vt->rangehigh = ctx->bands_rf->rangehigh;
  949. return 0;
  950. }
  951. static int max2175_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  952. {
  953. /* Check tuner index is valid */
  954. if (vt->index > 0)
  955. return -EINVAL;
  956. return 0;
  957. }
  958. static const struct v4l2_subdev_tuner_ops max2175_tuner_ops = {
  959. .s_frequency = max2175_s_frequency,
  960. .g_frequency = max2175_g_frequency,
  961. .enum_freq_bands = max2175_enum_freq_bands,
  962. .g_tuner = max2175_g_tuner,
  963. .s_tuner = max2175_s_tuner,
  964. };
  965. static const struct v4l2_subdev_ops max2175_ops = {
  966. .tuner = &max2175_tuner_ops,
  967. };
  968. static const struct v4l2_ctrl_ops max2175_ctrl_ops = {
  969. .s_ctrl = max2175_s_ctrl,
  970. .g_volatile_ctrl = max2175_g_volatile_ctrl,
  971. };
  972. /*
  973. * I2S output enable/disable configuration. This is a private control.
  974. * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
  975. */
  976. static const struct v4l2_ctrl_config max2175_i2s_en = {
  977. .ops = &max2175_ctrl_ops,
  978. .id = V4L2_CID_MAX2175_I2S_ENABLE,
  979. .name = "I2S Enable",
  980. .type = V4L2_CTRL_TYPE_BOOLEAN,
  981. .min = 0,
  982. .max = 1,
  983. .step = 1,
  984. .def = 1,
  985. .is_private = 1,
  986. };
  987. /*
  988. * HSLS value control LO freq adjacent location configuration.
  989. * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
  990. */
  991. static const struct v4l2_ctrl_config max2175_hsls = {
  992. .ops = &max2175_ctrl_ops,
  993. .id = V4L2_CID_MAX2175_HSLS,
  994. .name = "HSLS Above/Below Desired",
  995. .type = V4L2_CTRL_TYPE_BOOLEAN,
  996. .min = 0,
  997. .max = 1,
  998. .step = 1,
  999. .def = 1,
  1000. };
  1001. /*
  1002. * Rx modes below are a set of preset configurations that decides the tuner's
  1003. * sck and sample rate of transmission. They are separate for EU & NA regions.
  1004. * Refer to Documentation/userspace-api/media/drivers/max2175.rst for more details.
  1005. */
  1006. static const char * const max2175_ctrl_eu_rx_modes[] = {
  1007. [MAX2175_EU_FM_1_2] = "EU FM 1.2",
  1008. [MAX2175_DAB_1_2] = "DAB 1.2",
  1009. };
  1010. static const char * const max2175_ctrl_na_rx_modes[] = {
  1011. [MAX2175_NA_FM_1_0] = "NA FM 1.0",
  1012. [MAX2175_NA_FM_2_0] = "NA FM 2.0",
  1013. };
  1014. static const struct v4l2_ctrl_config max2175_eu_rx_mode = {
  1015. .ops = &max2175_ctrl_ops,
  1016. .id = V4L2_CID_MAX2175_RX_MODE,
  1017. .name = "RX Mode",
  1018. .type = V4L2_CTRL_TYPE_MENU,
  1019. .max = ARRAY_SIZE(max2175_ctrl_eu_rx_modes) - 1,
  1020. .def = 0,
  1021. .qmenu = max2175_ctrl_eu_rx_modes,
  1022. };
  1023. static const struct v4l2_ctrl_config max2175_na_rx_mode = {
  1024. .ops = &max2175_ctrl_ops,
  1025. .id = V4L2_CID_MAX2175_RX_MODE,
  1026. .name = "RX Mode",
  1027. .type = V4L2_CTRL_TYPE_MENU,
  1028. .max = ARRAY_SIZE(max2175_ctrl_na_rx_modes) - 1,
  1029. .def = 0,
  1030. .qmenu = max2175_ctrl_na_rx_modes,
  1031. };
  1032. static int max2175_refout_load_to_bits(struct i2c_client *client, u32 load,
  1033. u32 *bits)
  1034. {
  1035. if (load <= 40)
  1036. *bits = load / 10;
  1037. else if (load >= 60 && load <= 70)
  1038. *bits = load / 10 - 1;
  1039. else
  1040. return -EINVAL;
  1041. return 0;
  1042. }
  1043. static int max2175_probe(struct i2c_client *client)
  1044. {
  1045. bool master = true, am_hiz = false;
  1046. u32 refout_load, refout_bits = 0; /* REFOUT disabled */
  1047. struct v4l2_ctrl_handler *hdl;
  1048. struct fwnode_handle *fwnode;
  1049. struct device_node *np;
  1050. struct v4l2_subdev *sd;
  1051. struct regmap *regmap;
  1052. struct max2175 *ctx;
  1053. struct clk *clk;
  1054. int ret;
  1055. /* Parse DT properties */
  1056. np = of_parse_phandle(client->dev.of_node, "maxim,master", 0);
  1057. if (np) {
  1058. master = false; /* Slave tuner */
  1059. of_node_put(np);
  1060. }
  1061. fwnode = of_fwnode_handle(client->dev.of_node);
  1062. if (fwnode_property_present(fwnode, "maxim,am-hiz-filter"))
  1063. am_hiz = true;
  1064. if (!fwnode_property_read_u32(fwnode, "maxim,refout-load",
  1065. &refout_load)) {
  1066. ret = max2175_refout_load_to_bits(client, refout_load,
  1067. &refout_bits);
  1068. if (ret) {
  1069. dev_err(&client->dev, "invalid refout_load %u\n",
  1070. refout_load);
  1071. return -EINVAL;
  1072. }
  1073. }
  1074. clk = devm_clk_get(&client->dev, NULL);
  1075. if (IS_ERR(clk)) {
  1076. ret = PTR_ERR(clk);
  1077. dev_err(&client->dev, "cannot get clock %d\n", ret);
  1078. return ret;
  1079. }
  1080. regmap = devm_regmap_init_i2c(client, &max2175_regmap_config);
  1081. if (IS_ERR(regmap)) {
  1082. ret = PTR_ERR(regmap);
  1083. dev_err(&client->dev, "regmap init failed %d\n", ret);
  1084. return -ENODEV;
  1085. }
  1086. /* Alloc tuner context */
  1087. ctx = devm_kzalloc(&client->dev, sizeof(*ctx), GFP_KERNEL);
  1088. if (ctx == NULL)
  1089. return -ENOMEM;
  1090. sd = &ctx->sd;
  1091. ctx->master = master;
  1092. ctx->am_hiz = am_hiz;
  1093. ctx->mode_resolved = false;
  1094. ctx->regmap = regmap;
  1095. ctx->xtal_freq = clk_get_rate(clk);
  1096. dev_info(&client->dev, "xtal freq %luHz\n", ctx->xtal_freq);
  1097. v4l2_i2c_subdev_init(sd, client, &max2175_ops);
  1098. ctx->client = client;
  1099. sd->flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1100. /* Controls */
  1101. hdl = &ctx->ctrl_hdl;
  1102. ret = v4l2_ctrl_handler_init(hdl, 7);
  1103. if (ret)
  1104. return ret;
  1105. ctx->lna_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
  1106. V4L2_CID_RF_TUNER_LNA_GAIN,
  1107. 0, 63, 1, 0);
  1108. ctx->lna_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
  1109. V4L2_CTRL_FLAG_READ_ONLY);
  1110. ctx->if_gain = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
  1111. V4L2_CID_RF_TUNER_IF_GAIN,
  1112. 0, 31, 1, 0);
  1113. ctx->if_gain->flags |= (V4L2_CTRL_FLAG_VOLATILE |
  1114. V4L2_CTRL_FLAG_READ_ONLY);
  1115. ctx->pll_lock = v4l2_ctrl_new_std(hdl, &max2175_ctrl_ops,
  1116. V4L2_CID_RF_TUNER_PLL_LOCK,
  1117. 0, 1, 1, 0);
  1118. ctx->pll_lock->flags |= (V4L2_CTRL_FLAG_VOLATILE |
  1119. V4L2_CTRL_FLAG_READ_ONLY);
  1120. ctx->i2s_en = v4l2_ctrl_new_custom(hdl, &max2175_i2s_en, NULL);
  1121. ctx->hsls = v4l2_ctrl_new_custom(hdl, &max2175_hsls, NULL);
  1122. if (ctx->xtal_freq == MAX2175_EU_XTAL_FREQ) {
  1123. ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
  1124. &max2175_eu_rx_mode, NULL);
  1125. ctx->rx_modes = eu_rx_modes;
  1126. ctx->bands_rf = &eu_bands_rf;
  1127. } else {
  1128. ctx->rx_mode = v4l2_ctrl_new_custom(hdl,
  1129. &max2175_na_rx_mode, NULL);
  1130. ctx->rx_modes = na_rx_modes;
  1131. ctx->bands_rf = &na_bands_rf;
  1132. }
  1133. ctx->sd.ctrl_handler = &ctx->ctrl_hdl;
  1134. /* Set the defaults */
  1135. ctx->freq = ctx->bands_rf->rangelow;
  1136. /* Register subdev */
  1137. ret = v4l2_async_register_subdev(sd);
  1138. if (ret) {
  1139. dev_err(&client->dev, "register subdev failed\n");
  1140. goto err_reg;
  1141. }
  1142. /* Initialize device */
  1143. ret = max2175_core_init(ctx, refout_bits);
  1144. if (ret)
  1145. goto err_init;
  1146. ret = v4l2_ctrl_handler_setup(hdl);
  1147. if (ret)
  1148. goto err_init;
  1149. return 0;
  1150. err_init:
  1151. v4l2_async_unregister_subdev(sd);
  1152. err_reg:
  1153. v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  1154. return ret;
  1155. }
  1156. static void max2175_remove(struct i2c_client *client)
  1157. {
  1158. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1159. struct max2175 *ctx = max2175_from_sd(sd);
  1160. v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
  1161. v4l2_async_unregister_subdev(sd);
  1162. }
  1163. static const struct i2c_device_id max2175_id[] = {
  1164. { DRIVER_NAME },
  1165. {}
  1166. };
  1167. MODULE_DEVICE_TABLE(i2c, max2175_id);
  1168. static const struct of_device_id max2175_of_ids[] = {
  1169. { .compatible = "maxim,max2175", },
  1170. { }
  1171. };
  1172. MODULE_DEVICE_TABLE(of, max2175_of_ids);
  1173. static struct i2c_driver max2175_driver = {
  1174. .driver = {
  1175. .name = DRIVER_NAME,
  1176. .of_match_table = max2175_of_ids,
  1177. },
  1178. .probe = max2175_probe,
  1179. .remove = max2175_remove,
  1180. .id_table = max2175_id,
  1181. };
  1182. module_i2c_driver(max2175_driver);
  1183. MODULE_DESCRIPTION("Maxim MAX2175 RF to Bits tuner driver");
  1184. MODULE_LICENSE("GPL v2");
  1185. MODULE_AUTHOR("Ramesh Shanmugasundaram <ramesh.shanmugasundaram@bp.renesas.com>");