max2175.c 38 KB

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