max1363.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * iio/adc/max1363.c
  3. * Copyright (C) 2008-2010 Jonathan Cameron
  4. *
  5. * based on linux/drivers/i2c/chips/max123x
  6. * Copyright (C) 2002-2004 Stefan Eletzhofer
  7. *
  8. * based on linux/drivers/acron/char/pcf8583.c
  9. * Copyright (C) 2000 Russell King
  10. *
  11. * Driver for max1363 and similar chips.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/interrupt.h>
  18. #include <linux/device.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sysfs.h>
  21. #include <linux/list.h>
  22. #include <linux/i2c.h>
  23. #include <linux/regulator/consumer.h>
  24. #include <linux/slab.h>
  25. #include <linux/err.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include <linux/of_device.h>
  29. #include <linux/iio/iio.h>
  30. #include <linux/iio/sysfs.h>
  31. #include <linux/iio/events.h>
  32. #include <linux/iio/buffer.h>
  33. #include <linux/iio/driver.h>
  34. #include <linux/iio/kfifo_buf.h>
  35. #include <linux/iio/trigger_consumer.h>
  36. #include <linux/iio/triggered_buffer.h>
  37. #define MAX1363_SETUP_BYTE(a) ((a) | 0x80)
  38. /* There is a fair bit more defined here than currently
  39. * used, but the intention is to support everything these
  40. * chips do in the long run */
  41. /* see data sheets */
  42. /* max1363 and max1236, max1237, max1238, max1239 */
  43. #define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_VDD 0x00
  44. #define MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF 0x20
  45. #define MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT 0x40
  46. #define MAX1363_SETUP_AIN3_IS_REF_REF_IS_INT 0x60
  47. #define MAX1363_SETUP_POWER_UP_INT_REF 0x10
  48. #define MAX1363_SETUP_POWER_DOWN_INT_REF 0x00
  49. /* think about including max11600 etc - more settings */
  50. #define MAX1363_SETUP_EXT_CLOCK 0x08
  51. #define MAX1363_SETUP_INT_CLOCK 0x00
  52. #define MAX1363_SETUP_UNIPOLAR 0x00
  53. #define MAX1363_SETUP_BIPOLAR 0x04
  54. #define MAX1363_SETUP_RESET 0x00
  55. #define MAX1363_SETUP_NORESET 0x02
  56. /* max1363 only - though don't care on others.
  57. * For now monitor modes are not implemented as the relevant
  58. * line is not connected on my test board.
  59. * The definitions are here as I intend to add this soon.
  60. */
  61. #define MAX1363_SETUP_MONITOR_SETUP 0x01
  62. /* Specific to the max1363 */
  63. #define MAX1363_MON_RESET_CHAN(a) (1 << ((a) + 4))
  64. #define MAX1363_MON_INT_ENABLE 0x01
  65. /* defined for readability reasons */
  66. /* All chips */
  67. #define MAX1363_CONFIG_BYTE(a) ((a))
  68. #define MAX1363_CONFIG_SE 0x01
  69. #define MAX1363_CONFIG_DE 0x00
  70. #define MAX1363_CONFIG_SCAN_TO_CS 0x00
  71. #define MAX1363_CONFIG_SCAN_SINGLE_8 0x20
  72. #define MAX1363_CONFIG_SCAN_MONITOR_MODE 0x40
  73. #define MAX1363_CONFIG_SCAN_SINGLE_1 0x60
  74. /* max123{6-9} only */
  75. #define MAX1236_SCAN_MID_TO_CHANNEL 0x40
  76. /* max1363 only - merely part of channel selects or don't care for others */
  77. #define MAX1363_CONFIG_EN_MON_MODE_READ 0x18
  78. #define MAX1363_CHANNEL_SEL(a) ((a) << 1)
  79. /* max1363 strictly 0x06 - but doesn't matter */
  80. #define MAX1363_CHANNEL_SEL_MASK 0x1E
  81. #define MAX1363_SCAN_MASK 0x60
  82. #define MAX1363_SE_DE_MASK 0x01
  83. #define MAX1363_MAX_CHANNELS 25
  84. /**
  85. * struct max1363_mode - scan mode information
  86. * @conf: The corresponding value of the configuration register
  87. * @modemask: Bit mask corresponding to channels enabled in this mode
  88. */
  89. struct max1363_mode {
  90. int8_t conf;
  91. DECLARE_BITMAP(modemask, MAX1363_MAX_CHANNELS);
  92. };
  93. /* This must be maintained along side the max1363_mode_table in max1363_core */
  94. enum max1363_modes {
  95. /* Single read of a single channel */
  96. _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
  97. /* Differential single read */
  98. d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
  99. d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
  100. /* Scan to channel and mid to channel where overlapping */
  101. s0to1, s0to2, s2to3, s0to3, s0to4, s0to5, s0to6,
  102. s6to7, s0to7, s6to8, s0to8, s6to9,
  103. s0to9, s6to10, s0to10, s6to11, s0to11,
  104. /* Differential scan to channel and mid to channel where overlapping */
  105. d0m1to2m3, d0m1to4m5, d0m1to6m7, d6m7to8m9,
  106. d0m1to8m9, d6m7to10m11, d0m1to10m11, d1m0to3m2,
  107. d1m0to5m4, d1m0to7m6, d7m6to9m8, d1m0to9m8,
  108. d7m6to11m10, d1m0to11m10,
  109. };
  110. /**
  111. * struct max1363_chip_info - chip specifc information
  112. * @info: iio core function callbacks structure
  113. * @channels: channel specification
  114. * @num_channels: number of channels
  115. * @mode_list: array of available scan modes
  116. * @default_mode: the scan mode in which the chip starts up
  117. * @int_vref_mv: the internal reference voltage
  118. * @num_modes: number of modes
  119. * @bits: accuracy of the adc in bits
  120. */
  121. struct max1363_chip_info {
  122. const struct iio_info *info;
  123. const struct iio_chan_spec *channels;
  124. int num_channels;
  125. const enum max1363_modes *mode_list;
  126. enum max1363_modes default_mode;
  127. u16 int_vref_mv;
  128. u8 num_modes;
  129. u8 bits;
  130. };
  131. /**
  132. * struct max1363_state - driver instance specific data
  133. * @client: i2c_client
  134. * @setupbyte: cache of current device setup byte
  135. * @configbyte: cache of current device config byte
  136. * @chip_info: chip model specific constants, available modes, etc.
  137. * @current_mode: the scan mode of this chip
  138. * @requestedmask: a valid requested set of channels
  139. * @reg: supply regulator
  140. * @monitor_on: whether monitor mode is enabled
  141. * @monitor_speed: parameter corresponding to device monitor speed setting
  142. * @mask_high: bitmask for enabled high thresholds
  143. * @mask_low: bitmask for enabled low thresholds
  144. * @thresh_high: high threshold values
  145. * @thresh_low: low threshold values
  146. * @vref: Reference voltage regulator
  147. * @vref_uv: Actual (external or internal) reference voltage
  148. * @send: function used to send data to the chip
  149. * @recv: function used to receive data from the chip
  150. */
  151. struct max1363_state {
  152. struct i2c_client *client;
  153. u8 setupbyte;
  154. u8 configbyte;
  155. const struct max1363_chip_info *chip_info;
  156. const struct max1363_mode *current_mode;
  157. u32 requestedmask;
  158. struct regulator *reg;
  159. /* Using monitor modes and buffer at the same time is
  160. currently not supported */
  161. bool monitor_on;
  162. unsigned int monitor_speed:3;
  163. u8 mask_high;
  164. u8 mask_low;
  165. /* 4x unipolar first then the fours bipolar ones */
  166. s16 thresh_high[8];
  167. s16 thresh_low[8];
  168. struct regulator *vref;
  169. u32 vref_uv;
  170. int (*send)(const struct i2c_client *client,
  171. const char *buf, int count);
  172. int (*recv)(const struct i2c_client *client,
  173. char *buf, int count);
  174. };
  175. #define MAX1363_MODE_SINGLE(_num, _mask) { \
  176. .conf = MAX1363_CHANNEL_SEL(_num) \
  177. | MAX1363_CONFIG_SCAN_SINGLE_1 \
  178. | MAX1363_CONFIG_SE, \
  179. .modemask[0] = _mask, \
  180. }
  181. #define MAX1363_MODE_SCAN_TO_CHANNEL(_num, _mask) { \
  182. .conf = MAX1363_CHANNEL_SEL(_num) \
  183. | MAX1363_CONFIG_SCAN_TO_CS \
  184. | MAX1363_CONFIG_SE, \
  185. .modemask[0] = _mask, \
  186. }
  187. /* note not available for max1363 hence naming */
  188. #define MAX1236_MODE_SCAN_MID_TO_CHANNEL(_mid, _num, _mask) { \
  189. .conf = MAX1363_CHANNEL_SEL(_num) \
  190. | MAX1236_SCAN_MID_TO_CHANNEL \
  191. | MAX1363_CONFIG_SE, \
  192. .modemask[0] = _mask \
  193. }
  194. #define MAX1363_MODE_DIFF_SINGLE(_nump, _numm, _mask) { \
  195. .conf = MAX1363_CHANNEL_SEL(_nump) \
  196. | MAX1363_CONFIG_SCAN_SINGLE_1 \
  197. | MAX1363_CONFIG_DE, \
  198. .modemask[0] = _mask \
  199. }
  200. /* Can't think how to automate naming so specify for now */
  201. #define MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(_num, _numvals, _mask) { \
  202. .conf = MAX1363_CHANNEL_SEL(_num) \
  203. | MAX1363_CONFIG_SCAN_TO_CS \
  204. | MAX1363_CONFIG_DE, \
  205. .modemask[0] = _mask \
  206. }
  207. /* note only available for max1363 hence naming */
  208. #define MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(_num, _numvals, _mask) { \
  209. .conf = MAX1363_CHANNEL_SEL(_num) \
  210. | MAX1236_SCAN_MID_TO_CHANNEL \
  211. | MAX1363_CONFIG_SE, \
  212. .modemask[0] = _mask \
  213. }
  214. static const struct max1363_mode max1363_mode_table[] = {
  215. /* All of the single channel options first */
  216. MAX1363_MODE_SINGLE(0, 1 << 0),
  217. MAX1363_MODE_SINGLE(1, 1 << 1),
  218. MAX1363_MODE_SINGLE(2, 1 << 2),
  219. MAX1363_MODE_SINGLE(3, 1 << 3),
  220. MAX1363_MODE_SINGLE(4, 1 << 4),
  221. MAX1363_MODE_SINGLE(5, 1 << 5),
  222. MAX1363_MODE_SINGLE(6, 1 << 6),
  223. MAX1363_MODE_SINGLE(7, 1 << 7),
  224. MAX1363_MODE_SINGLE(8, 1 << 8),
  225. MAX1363_MODE_SINGLE(9, 1 << 9),
  226. MAX1363_MODE_SINGLE(10, 1 << 10),
  227. MAX1363_MODE_SINGLE(11, 1 << 11),
  228. MAX1363_MODE_DIFF_SINGLE(0, 1, 1 << 12),
  229. MAX1363_MODE_DIFF_SINGLE(2, 3, 1 << 13),
  230. MAX1363_MODE_DIFF_SINGLE(4, 5, 1 << 14),
  231. MAX1363_MODE_DIFF_SINGLE(6, 7, 1 << 15),
  232. MAX1363_MODE_DIFF_SINGLE(8, 9, 1 << 16),
  233. MAX1363_MODE_DIFF_SINGLE(10, 11, 1 << 17),
  234. MAX1363_MODE_DIFF_SINGLE(1, 0, 1 << 18),
  235. MAX1363_MODE_DIFF_SINGLE(3, 2, 1 << 19),
  236. MAX1363_MODE_DIFF_SINGLE(5, 4, 1 << 20),
  237. MAX1363_MODE_DIFF_SINGLE(7, 6, 1 << 21),
  238. MAX1363_MODE_DIFF_SINGLE(9, 8, 1 << 22),
  239. MAX1363_MODE_DIFF_SINGLE(11, 10, 1 << 23),
  240. /* The multichannel scans next */
  241. MAX1363_MODE_SCAN_TO_CHANNEL(1, 0x003),
  242. MAX1363_MODE_SCAN_TO_CHANNEL(2, 0x007),
  243. MAX1236_MODE_SCAN_MID_TO_CHANNEL(2, 3, 0x00C),
  244. MAX1363_MODE_SCAN_TO_CHANNEL(3, 0x00F),
  245. MAX1363_MODE_SCAN_TO_CHANNEL(4, 0x01F),
  246. MAX1363_MODE_SCAN_TO_CHANNEL(5, 0x03F),
  247. MAX1363_MODE_SCAN_TO_CHANNEL(6, 0x07F),
  248. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 7, 0x0C0),
  249. MAX1363_MODE_SCAN_TO_CHANNEL(7, 0x0FF),
  250. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 8, 0x1C0),
  251. MAX1363_MODE_SCAN_TO_CHANNEL(8, 0x1FF),
  252. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 9, 0x3C0),
  253. MAX1363_MODE_SCAN_TO_CHANNEL(9, 0x3FF),
  254. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 10, 0x7C0),
  255. MAX1363_MODE_SCAN_TO_CHANNEL(10, 0x7FF),
  256. MAX1236_MODE_SCAN_MID_TO_CHANNEL(6, 11, 0xFC0),
  257. MAX1363_MODE_SCAN_TO_CHANNEL(11, 0xFFF),
  258. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(2, 2, 0x003000),
  259. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(4, 3, 0x007000),
  260. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(6, 4, 0x00F000),
  261. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(8, 2, 0x018000),
  262. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(8, 5, 0x01F000),
  263. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(10, 3, 0x038000),
  264. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(10, 6, 0x3F000),
  265. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(3, 2, 0x0C0000),
  266. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(5, 3, 0x1C0000),
  267. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(7, 4, 0x3C0000),
  268. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(9, 2, 0x600000),
  269. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(9, 5, 0x7C0000),
  270. MAX1236_MODE_DIFF_SCAN_MID_TO_CHANNEL(11, 3, 0xE00000),
  271. MAX1363_MODE_DIFF_SCAN_TO_CHANNEL(11, 6, 0xFC0000),
  272. };
  273. static const struct max1363_mode
  274. *max1363_match_mode(const unsigned long *mask,
  275. const struct max1363_chip_info *ci)
  276. {
  277. int i;
  278. if (mask)
  279. for (i = 0; i < ci->num_modes; i++)
  280. if (bitmap_subset(mask,
  281. max1363_mode_table[ci->mode_list[i]].
  282. modemask,
  283. MAX1363_MAX_CHANNELS))
  284. return &max1363_mode_table[ci->mode_list[i]];
  285. return NULL;
  286. }
  287. static int max1363_smbus_send(const struct i2c_client *client, const char *buf,
  288. int count)
  289. {
  290. int i, err;
  291. for (i = err = 0; err == 0 && i < count; ++i)
  292. err = i2c_smbus_write_byte(client, buf[i]);
  293. return err ? err : count;
  294. }
  295. static int max1363_smbus_recv(const struct i2c_client *client, char *buf,
  296. int count)
  297. {
  298. int i, ret;
  299. for (i = 0; i < count; ++i) {
  300. ret = i2c_smbus_read_byte(client);
  301. if (ret < 0)
  302. return ret;
  303. buf[i] = ret;
  304. }
  305. return count;
  306. }
  307. static int max1363_write_basic_config(struct max1363_state *st)
  308. {
  309. u8 tx_buf[2] = { st->setupbyte, st->configbyte };
  310. return st->send(st->client, tx_buf, 2);
  311. }
  312. static int max1363_set_scan_mode(struct max1363_state *st)
  313. {
  314. st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
  315. | MAX1363_SCAN_MASK
  316. | MAX1363_SE_DE_MASK);
  317. st->configbyte |= st->current_mode->conf;
  318. return max1363_write_basic_config(st);
  319. }
  320. static int max1363_read_single_chan(struct iio_dev *indio_dev,
  321. struct iio_chan_spec const *chan,
  322. int *val,
  323. long m)
  324. {
  325. int ret = 0;
  326. s32 data;
  327. u8 rxbuf[2];
  328. struct max1363_state *st = iio_priv(indio_dev);
  329. struct i2c_client *client = st->client;
  330. mutex_lock(&indio_dev->mlock);
  331. /*
  332. * If monitor mode is enabled, the method for reading a single
  333. * channel will have to be rather different and has not yet
  334. * been implemented.
  335. *
  336. * Also, cannot read directly if buffered capture enabled.
  337. */
  338. if (st->monitor_on || iio_buffer_enabled(indio_dev)) {
  339. ret = -EBUSY;
  340. goto error_ret;
  341. }
  342. /* Check to see if current scan mode is correct */
  343. if (st->current_mode != &max1363_mode_table[chan->address]) {
  344. /* Update scan mode if needed */
  345. st->current_mode = &max1363_mode_table[chan->address];
  346. ret = max1363_set_scan_mode(st);
  347. if (ret < 0)
  348. goto error_ret;
  349. }
  350. if (st->chip_info->bits != 8) {
  351. /* Get reading */
  352. data = st->recv(client, rxbuf, 2);
  353. if (data < 0) {
  354. ret = data;
  355. goto error_ret;
  356. }
  357. data = (rxbuf[1] | rxbuf[0] << 8) &
  358. ((1 << st->chip_info->bits) - 1);
  359. } else {
  360. /* Get reading */
  361. data = st->recv(client, rxbuf, 1);
  362. if (data < 0) {
  363. ret = data;
  364. goto error_ret;
  365. }
  366. data = rxbuf[0];
  367. }
  368. *val = data;
  369. error_ret:
  370. mutex_unlock(&indio_dev->mlock);
  371. return ret;
  372. }
  373. static int max1363_read_raw(struct iio_dev *indio_dev,
  374. struct iio_chan_spec const *chan,
  375. int *val,
  376. int *val2,
  377. long m)
  378. {
  379. struct max1363_state *st = iio_priv(indio_dev);
  380. int ret;
  381. switch (m) {
  382. case IIO_CHAN_INFO_RAW:
  383. ret = max1363_read_single_chan(indio_dev, chan, val, m);
  384. if (ret < 0)
  385. return ret;
  386. return IIO_VAL_INT;
  387. case IIO_CHAN_INFO_SCALE:
  388. *val = st->vref_uv / 1000;
  389. *val2 = st->chip_info->bits;
  390. return IIO_VAL_FRACTIONAL_LOG2;
  391. default:
  392. return -EINVAL;
  393. }
  394. return 0;
  395. }
  396. /* Applies to max1363 */
  397. static const enum max1363_modes max1363_mode_list[] = {
  398. _s0, _s1, _s2, _s3,
  399. s0to1, s0to2, s0to3,
  400. d0m1, d2m3, d1m0, d3m2,
  401. d0m1to2m3, d1m0to3m2,
  402. };
  403. static const struct iio_event_spec max1363_events[] = {
  404. {
  405. .type = IIO_EV_TYPE_THRESH,
  406. .dir = IIO_EV_DIR_RISING,
  407. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  408. BIT(IIO_EV_INFO_ENABLE),
  409. }, {
  410. .type = IIO_EV_TYPE_THRESH,
  411. .dir = IIO_EV_DIR_FALLING,
  412. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  413. BIT(IIO_EV_INFO_ENABLE),
  414. },
  415. };
  416. #define MAX1363_CHAN_U(num, addr, si, bits, ev_spec, num_ev_spec) \
  417. { \
  418. .type = IIO_VOLTAGE, \
  419. .indexed = 1, \
  420. .channel = num, \
  421. .address = addr, \
  422. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  423. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  424. .datasheet_name = "AIN"#num, \
  425. .scan_type = { \
  426. .sign = 'u', \
  427. .realbits = bits, \
  428. .storagebits = (bits > 8) ? 16 : 8, \
  429. .endianness = IIO_BE, \
  430. }, \
  431. .scan_index = si, \
  432. .event_spec = ev_spec, \
  433. .num_event_specs = num_ev_spec, \
  434. }
  435. /* bipolar channel */
  436. #define MAX1363_CHAN_B(num, num2, addr, si, bits, ev_spec, num_ev_spec) \
  437. { \
  438. .type = IIO_VOLTAGE, \
  439. .differential = 1, \
  440. .indexed = 1, \
  441. .channel = num, \
  442. .channel2 = num2, \
  443. .address = addr, \
  444. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  445. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
  446. .datasheet_name = "AIN"#num"-AIN"#num2, \
  447. .scan_type = { \
  448. .sign = 's', \
  449. .realbits = bits, \
  450. .storagebits = (bits > 8) ? 16 : 8, \
  451. .endianness = IIO_BE, \
  452. }, \
  453. .scan_index = si, \
  454. .event_spec = ev_spec, \
  455. .num_event_specs = num_ev_spec, \
  456. }
  457. #define MAX1363_4X_CHANS(bits, ev_spec, num_ev_spec) { \
  458. MAX1363_CHAN_U(0, _s0, 0, bits, ev_spec, num_ev_spec), \
  459. MAX1363_CHAN_U(1, _s1, 1, bits, ev_spec, num_ev_spec), \
  460. MAX1363_CHAN_U(2, _s2, 2, bits, ev_spec, num_ev_spec), \
  461. MAX1363_CHAN_U(3, _s3, 3, bits, ev_spec, num_ev_spec), \
  462. MAX1363_CHAN_B(0, 1, d0m1, 4, bits, ev_spec, num_ev_spec), \
  463. MAX1363_CHAN_B(2, 3, d2m3, 5, bits, ev_spec, num_ev_spec), \
  464. MAX1363_CHAN_B(1, 0, d1m0, 6, bits, ev_spec, num_ev_spec), \
  465. MAX1363_CHAN_B(3, 2, d3m2, 7, bits, ev_spec, num_ev_spec), \
  466. IIO_CHAN_SOFT_TIMESTAMP(8) \
  467. }
  468. static const struct iio_chan_spec max1036_channels[] =
  469. MAX1363_4X_CHANS(8, NULL, 0);
  470. static const struct iio_chan_spec max1136_channels[] =
  471. MAX1363_4X_CHANS(10, NULL, 0);
  472. static const struct iio_chan_spec max1236_channels[] =
  473. MAX1363_4X_CHANS(12, NULL, 0);
  474. static const struct iio_chan_spec max1361_channels[] =
  475. MAX1363_4X_CHANS(10, max1363_events, ARRAY_SIZE(max1363_events));
  476. static const struct iio_chan_spec max1363_channels[] =
  477. MAX1363_4X_CHANS(12, max1363_events, ARRAY_SIZE(max1363_events));
  478. /* Applies to max1236, max1237 */
  479. static const enum max1363_modes max1236_mode_list[] = {
  480. _s0, _s1, _s2, _s3,
  481. s0to1, s0to2, s0to3,
  482. d0m1, d2m3, d1m0, d3m2,
  483. d0m1to2m3, d1m0to3m2,
  484. s2to3,
  485. };
  486. /* Applies to max1238, max1239 */
  487. static const enum max1363_modes max1238_mode_list[] = {
  488. _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7, _s8, _s9, _s10, _s11,
  489. s0to1, s0to2, s0to3, s0to4, s0to5, s0to6,
  490. s0to7, s0to8, s0to9, s0to10, s0to11,
  491. d0m1, d2m3, d4m5, d6m7, d8m9, d10m11,
  492. d1m0, d3m2, d5m4, d7m6, d9m8, d11m10,
  493. d0m1to2m3, d0m1to4m5, d0m1to6m7, d0m1to8m9, d0m1to10m11,
  494. d1m0to3m2, d1m0to5m4, d1m0to7m6, d1m0to9m8, d1m0to11m10,
  495. s6to7, s6to8, s6to9, s6to10, s6to11,
  496. d6m7to8m9, d6m7to10m11, d7m6to9m8, d7m6to11m10,
  497. };
  498. #define MAX1363_12X_CHANS(bits) { \
  499. MAX1363_CHAN_U(0, _s0, 0, bits, NULL, 0), \
  500. MAX1363_CHAN_U(1, _s1, 1, bits, NULL, 0), \
  501. MAX1363_CHAN_U(2, _s2, 2, bits, NULL, 0), \
  502. MAX1363_CHAN_U(3, _s3, 3, bits, NULL, 0), \
  503. MAX1363_CHAN_U(4, _s4, 4, bits, NULL, 0), \
  504. MAX1363_CHAN_U(5, _s5, 5, bits, NULL, 0), \
  505. MAX1363_CHAN_U(6, _s6, 6, bits, NULL, 0), \
  506. MAX1363_CHAN_U(7, _s7, 7, bits, NULL, 0), \
  507. MAX1363_CHAN_U(8, _s8, 8, bits, NULL, 0), \
  508. MAX1363_CHAN_U(9, _s9, 9, bits, NULL, 0), \
  509. MAX1363_CHAN_U(10, _s10, 10, bits, NULL, 0), \
  510. MAX1363_CHAN_U(11, _s11, 11, bits, NULL, 0), \
  511. MAX1363_CHAN_B(0, 1, d0m1, 12, bits, NULL, 0), \
  512. MAX1363_CHAN_B(2, 3, d2m3, 13, bits, NULL, 0), \
  513. MAX1363_CHAN_B(4, 5, d4m5, 14, bits, NULL, 0), \
  514. MAX1363_CHAN_B(6, 7, d6m7, 15, bits, NULL, 0), \
  515. MAX1363_CHAN_B(8, 9, d8m9, 16, bits, NULL, 0), \
  516. MAX1363_CHAN_B(10, 11, d10m11, 17, bits, NULL, 0), \
  517. MAX1363_CHAN_B(1, 0, d1m0, 18, bits, NULL, 0), \
  518. MAX1363_CHAN_B(3, 2, d3m2, 19, bits, NULL, 0), \
  519. MAX1363_CHAN_B(5, 4, d5m4, 20, bits, NULL, 0), \
  520. MAX1363_CHAN_B(7, 6, d7m6, 21, bits, NULL, 0), \
  521. MAX1363_CHAN_B(9, 8, d9m8, 22, bits, NULL, 0), \
  522. MAX1363_CHAN_B(11, 10, d11m10, 23, bits, NULL, 0), \
  523. IIO_CHAN_SOFT_TIMESTAMP(24) \
  524. }
  525. static const struct iio_chan_spec max1038_channels[] = MAX1363_12X_CHANS(8);
  526. static const struct iio_chan_spec max1138_channels[] = MAX1363_12X_CHANS(10);
  527. static const struct iio_chan_spec max1238_channels[] = MAX1363_12X_CHANS(12);
  528. static const enum max1363_modes max11607_mode_list[] = {
  529. _s0, _s1, _s2, _s3,
  530. s0to1, s0to2, s0to3,
  531. s2to3,
  532. d0m1, d2m3, d1m0, d3m2,
  533. d0m1to2m3, d1m0to3m2,
  534. };
  535. static const enum max1363_modes max11608_mode_list[] = {
  536. _s0, _s1, _s2, _s3, _s4, _s5, _s6, _s7,
  537. s0to1, s0to2, s0to3, s0to4, s0to5, s0to6, s0to7,
  538. s6to7,
  539. d0m1, d2m3, d4m5, d6m7,
  540. d1m0, d3m2, d5m4, d7m6,
  541. d0m1to2m3, d0m1to4m5, d0m1to6m7,
  542. d1m0to3m2, d1m0to5m4, d1m0to7m6,
  543. };
  544. #define MAX1363_8X_CHANS(bits) { \
  545. MAX1363_CHAN_U(0, _s0, 0, bits, NULL, 0), \
  546. MAX1363_CHAN_U(1, _s1, 1, bits, NULL, 0), \
  547. MAX1363_CHAN_U(2, _s2, 2, bits, NULL, 0), \
  548. MAX1363_CHAN_U(3, _s3, 3, bits, NULL, 0), \
  549. MAX1363_CHAN_U(4, _s4, 4, bits, NULL, 0), \
  550. MAX1363_CHAN_U(5, _s5, 5, bits, NULL, 0), \
  551. MAX1363_CHAN_U(6, _s6, 6, bits, NULL, 0), \
  552. MAX1363_CHAN_U(7, _s7, 7, bits, NULL, 0), \
  553. MAX1363_CHAN_B(0, 1, d0m1, 8, bits, NULL, 0), \
  554. MAX1363_CHAN_B(2, 3, d2m3, 9, bits, NULL, 0), \
  555. MAX1363_CHAN_B(4, 5, d4m5, 10, bits, NULL, 0), \
  556. MAX1363_CHAN_B(6, 7, d6m7, 11, bits, NULL, 0), \
  557. MAX1363_CHAN_B(1, 0, d1m0, 12, bits, NULL, 0), \
  558. MAX1363_CHAN_B(3, 2, d3m2, 13, bits, NULL, 0), \
  559. MAX1363_CHAN_B(5, 4, d5m4, 14, bits, NULL, 0), \
  560. MAX1363_CHAN_B(7, 6, d7m6, 15, bits, NULL, 0), \
  561. IIO_CHAN_SOFT_TIMESTAMP(16) \
  562. }
  563. static const struct iio_chan_spec max11602_channels[] = MAX1363_8X_CHANS(8);
  564. static const struct iio_chan_spec max11608_channels[] = MAX1363_8X_CHANS(10);
  565. static const struct iio_chan_spec max11614_channels[] = MAX1363_8X_CHANS(12);
  566. static const enum max1363_modes max11644_mode_list[] = {
  567. _s0, _s1, s0to1, d0m1, d1m0,
  568. };
  569. #define MAX1363_2X_CHANS(bits) { \
  570. MAX1363_CHAN_U(0, _s0, 0, bits, NULL, 0), \
  571. MAX1363_CHAN_U(1, _s1, 1, bits, NULL, 0), \
  572. MAX1363_CHAN_B(0, 1, d0m1, 2, bits, NULL, 0), \
  573. MAX1363_CHAN_B(1, 0, d1m0, 3, bits, NULL, 0), \
  574. IIO_CHAN_SOFT_TIMESTAMP(4) \
  575. }
  576. static const struct iio_chan_spec max11646_channels[] = MAX1363_2X_CHANS(10);
  577. static const struct iio_chan_spec max11644_channels[] = MAX1363_2X_CHANS(12);
  578. enum { max1361,
  579. max1362,
  580. max1363,
  581. max1364,
  582. max1036,
  583. max1037,
  584. max1038,
  585. max1039,
  586. max1136,
  587. max1137,
  588. max1138,
  589. max1139,
  590. max1236,
  591. max1237,
  592. max1238,
  593. max1239,
  594. max11600,
  595. max11601,
  596. max11602,
  597. max11603,
  598. max11604,
  599. max11605,
  600. max11606,
  601. max11607,
  602. max11608,
  603. max11609,
  604. max11610,
  605. max11611,
  606. max11612,
  607. max11613,
  608. max11614,
  609. max11615,
  610. max11616,
  611. max11617,
  612. max11644,
  613. max11645,
  614. max11646,
  615. max11647
  616. };
  617. static const int max1363_monitor_speeds[] = { 133000, 665000, 33300, 16600,
  618. 8300, 4200, 2000, 1000 };
  619. static ssize_t max1363_monitor_show_freq(struct device *dev,
  620. struct device_attribute *attr,
  621. char *buf)
  622. {
  623. struct max1363_state *st = iio_priv(dev_to_iio_dev(dev));
  624. return sprintf(buf, "%d\n", max1363_monitor_speeds[st->monitor_speed]);
  625. }
  626. static ssize_t max1363_monitor_store_freq(struct device *dev,
  627. struct device_attribute *attr,
  628. const char *buf,
  629. size_t len)
  630. {
  631. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  632. struct max1363_state *st = iio_priv(indio_dev);
  633. int i, ret;
  634. unsigned long val;
  635. bool found = false;
  636. ret = kstrtoul(buf, 10, &val);
  637. if (ret)
  638. return -EINVAL;
  639. for (i = 0; i < ARRAY_SIZE(max1363_monitor_speeds); i++)
  640. if (val == max1363_monitor_speeds[i]) {
  641. found = true;
  642. break;
  643. }
  644. if (!found)
  645. return -EINVAL;
  646. mutex_lock(&indio_dev->mlock);
  647. st->monitor_speed = i;
  648. mutex_unlock(&indio_dev->mlock);
  649. return 0;
  650. }
  651. static IIO_DEV_ATTR_SAMP_FREQ(S_IRUGO | S_IWUSR,
  652. max1363_monitor_show_freq,
  653. max1363_monitor_store_freq);
  654. static IIO_CONST_ATTR(sampling_frequency_available,
  655. "133000 665000 33300 16600 8300 4200 2000 1000");
  656. static int max1363_read_thresh(struct iio_dev *indio_dev,
  657. const struct iio_chan_spec *chan, enum iio_event_type type,
  658. enum iio_event_direction dir, enum iio_event_info info, int *val,
  659. int *val2)
  660. {
  661. struct max1363_state *st = iio_priv(indio_dev);
  662. if (dir == IIO_EV_DIR_FALLING)
  663. *val = st->thresh_low[chan->channel];
  664. else
  665. *val = st->thresh_high[chan->channel];
  666. return IIO_VAL_INT;
  667. }
  668. static int max1363_write_thresh(struct iio_dev *indio_dev,
  669. const struct iio_chan_spec *chan, enum iio_event_type type,
  670. enum iio_event_direction dir, enum iio_event_info info, int val,
  671. int val2)
  672. {
  673. struct max1363_state *st = iio_priv(indio_dev);
  674. /* make it handle signed correctly as well */
  675. switch (st->chip_info->bits) {
  676. case 10:
  677. if (val > 0x3FF)
  678. return -EINVAL;
  679. break;
  680. case 12:
  681. if (val > 0xFFF)
  682. return -EINVAL;
  683. break;
  684. }
  685. switch (dir) {
  686. case IIO_EV_DIR_FALLING:
  687. st->thresh_low[chan->channel] = val;
  688. break;
  689. case IIO_EV_DIR_RISING:
  690. st->thresh_high[chan->channel] = val;
  691. break;
  692. default:
  693. return -EINVAL;
  694. }
  695. return 0;
  696. }
  697. static const u64 max1363_event_codes[] = {
  698. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
  699. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  700. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
  701. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  702. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
  703. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  704. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
  705. IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
  706. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 0,
  707. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  708. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
  709. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  710. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
  711. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  712. IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
  713. IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
  714. };
  715. static irqreturn_t max1363_event_handler(int irq, void *private)
  716. {
  717. struct iio_dev *indio_dev = private;
  718. struct max1363_state *st = iio_priv(indio_dev);
  719. s64 timestamp = iio_get_time_ns(indio_dev);
  720. unsigned long mask, loc;
  721. u8 rx;
  722. u8 tx[2] = { st->setupbyte,
  723. MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0 };
  724. st->recv(st->client, &rx, 1);
  725. mask = rx;
  726. for_each_set_bit(loc, &mask, 8)
  727. iio_push_event(indio_dev, max1363_event_codes[loc], timestamp);
  728. st->send(st->client, tx, 2);
  729. return IRQ_HANDLED;
  730. }
  731. static int max1363_read_event_config(struct iio_dev *indio_dev,
  732. const struct iio_chan_spec *chan, enum iio_event_type type,
  733. enum iio_event_direction dir)
  734. {
  735. struct max1363_state *st = iio_priv(indio_dev);
  736. int val;
  737. int number = chan->channel;
  738. mutex_lock(&indio_dev->mlock);
  739. if (dir == IIO_EV_DIR_FALLING)
  740. val = (1 << number) & st->mask_low;
  741. else
  742. val = (1 << number) & st->mask_high;
  743. mutex_unlock(&indio_dev->mlock);
  744. return val;
  745. }
  746. static int max1363_monitor_mode_update(struct max1363_state *st, int enabled)
  747. {
  748. u8 *tx_buf;
  749. int ret, i = 3, j;
  750. unsigned long numelements;
  751. int len;
  752. const long *modemask;
  753. if (!enabled) {
  754. /* transition to buffered capture is not currently supported */
  755. st->setupbyte &= ~MAX1363_SETUP_MONITOR_SETUP;
  756. st->configbyte &= ~MAX1363_SCAN_MASK;
  757. st->monitor_on = false;
  758. return max1363_write_basic_config(st);
  759. }
  760. /* Ensure we are in the relevant mode */
  761. st->setupbyte |= MAX1363_SETUP_MONITOR_SETUP;
  762. st->configbyte &= ~(MAX1363_CHANNEL_SEL_MASK
  763. | MAX1363_SCAN_MASK
  764. | MAX1363_SE_DE_MASK);
  765. st->configbyte |= MAX1363_CONFIG_SCAN_MONITOR_MODE;
  766. if ((st->mask_low | st->mask_high) & 0x0F) {
  767. st->configbyte |= max1363_mode_table[s0to3].conf;
  768. modemask = max1363_mode_table[s0to3].modemask;
  769. } else if ((st->mask_low | st->mask_high) & 0x30) {
  770. st->configbyte |= max1363_mode_table[d0m1to2m3].conf;
  771. modemask = max1363_mode_table[d0m1to2m3].modemask;
  772. } else {
  773. st->configbyte |= max1363_mode_table[d1m0to3m2].conf;
  774. modemask = max1363_mode_table[d1m0to3m2].modemask;
  775. }
  776. numelements = bitmap_weight(modemask, MAX1363_MAX_CHANNELS);
  777. len = 3 * numelements + 3;
  778. tx_buf = kmalloc(len, GFP_KERNEL);
  779. if (!tx_buf) {
  780. ret = -ENOMEM;
  781. goto error_ret;
  782. }
  783. tx_buf[0] = st->configbyte;
  784. tx_buf[1] = st->setupbyte;
  785. tx_buf[2] = (st->monitor_speed << 1);
  786. /*
  787. * So we need to do yet another bit of nefarious scan mode
  788. * setup to match what we need.
  789. */
  790. for (j = 0; j < 8; j++)
  791. if (test_bit(j, modemask)) {
  792. /* Establish the mode is in the scan */
  793. if (st->mask_low & (1 << j)) {
  794. tx_buf[i] = (st->thresh_low[j] >> 4) & 0xFF;
  795. tx_buf[i + 1] = (st->thresh_low[j] << 4) & 0xF0;
  796. } else if (j < 4) {
  797. tx_buf[i] = 0;
  798. tx_buf[i + 1] = 0;
  799. } else {
  800. tx_buf[i] = 0x80;
  801. tx_buf[i + 1] = 0;
  802. }
  803. if (st->mask_high & (1 << j)) {
  804. tx_buf[i + 1] |=
  805. (st->thresh_high[j] >> 8) & 0x0F;
  806. tx_buf[i + 2] = st->thresh_high[j] & 0xFF;
  807. } else if (j < 4) {
  808. tx_buf[i + 1] |= 0x0F;
  809. tx_buf[i + 2] = 0xFF;
  810. } else {
  811. tx_buf[i + 1] |= 0x07;
  812. tx_buf[i + 2] = 0xFF;
  813. }
  814. i += 3;
  815. }
  816. ret = st->send(st->client, tx_buf, len);
  817. if (ret < 0)
  818. goto error_ret;
  819. if (ret != len) {
  820. ret = -EIO;
  821. goto error_ret;
  822. }
  823. /*
  824. * Now that we hopefully have sensible thresholds in place it is
  825. * time to turn the interrupts on.
  826. * It is unclear from the data sheet if this should be necessary
  827. * (i.e. whether monitor mode setup is atomic) but it appears to
  828. * be in practice.
  829. */
  830. tx_buf[0] = st->setupbyte;
  831. tx_buf[1] = MAX1363_MON_INT_ENABLE | (st->monitor_speed << 1) | 0xF0;
  832. ret = st->send(st->client, tx_buf, 2);
  833. if (ret < 0)
  834. goto error_ret;
  835. if (ret != 2) {
  836. ret = -EIO;
  837. goto error_ret;
  838. }
  839. ret = 0;
  840. st->monitor_on = true;
  841. error_ret:
  842. kfree(tx_buf);
  843. return ret;
  844. }
  845. /*
  846. * To keep this manageable we always use one of 3 scan modes.
  847. * Scan 0...3, 0-1,2-3 and 1-0,3-2
  848. */
  849. static inline int __max1363_check_event_mask(int thismask, int checkmask)
  850. {
  851. int ret = 0;
  852. /* Is it unipolar */
  853. if (thismask < 4) {
  854. if (checkmask & ~0x0F) {
  855. ret = -EBUSY;
  856. goto error_ret;
  857. }
  858. } else if (thismask < 6) {
  859. if (checkmask & ~0x30) {
  860. ret = -EBUSY;
  861. goto error_ret;
  862. }
  863. } else if (checkmask & ~0xC0)
  864. ret = -EBUSY;
  865. error_ret:
  866. return ret;
  867. }
  868. static int max1363_write_event_config(struct iio_dev *indio_dev,
  869. const struct iio_chan_spec *chan, enum iio_event_type type,
  870. enum iio_event_direction dir, int state)
  871. {
  872. int ret = 0;
  873. struct max1363_state *st = iio_priv(indio_dev);
  874. u16 unifiedmask;
  875. int number = chan->channel;
  876. mutex_lock(&indio_dev->mlock);
  877. unifiedmask = st->mask_low | st->mask_high;
  878. if (dir == IIO_EV_DIR_FALLING) {
  879. if (state == 0)
  880. st->mask_low &= ~(1 << number);
  881. else {
  882. ret = __max1363_check_event_mask((1 << number),
  883. unifiedmask);
  884. if (ret)
  885. goto error_ret;
  886. st->mask_low |= (1 << number);
  887. }
  888. } else {
  889. if (state == 0)
  890. st->mask_high &= ~(1 << number);
  891. else {
  892. ret = __max1363_check_event_mask((1 << number),
  893. unifiedmask);
  894. if (ret)
  895. goto error_ret;
  896. st->mask_high |= (1 << number);
  897. }
  898. }
  899. max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low));
  900. error_ret:
  901. mutex_unlock(&indio_dev->mlock);
  902. return ret;
  903. }
  904. /*
  905. * As with scan_elements, only certain sets of these can
  906. * be combined.
  907. */
  908. static struct attribute *max1363_event_attributes[] = {
  909. &iio_dev_attr_sampling_frequency.dev_attr.attr,
  910. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  911. NULL,
  912. };
  913. static struct attribute_group max1363_event_attribute_group = {
  914. .attrs = max1363_event_attributes,
  915. };
  916. static int max1363_update_scan_mode(struct iio_dev *indio_dev,
  917. const unsigned long *scan_mask)
  918. {
  919. struct max1363_state *st = iio_priv(indio_dev);
  920. /*
  921. * Need to figure out the current mode based upon the requested
  922. * scan mask in iio_dev
  923. */
  924. st->current_mode = max1363_match_mode(scan_mask, st->chip_info);
  925. if (!st->current_mode)
  926. return -EINVAL;
  927. max1363_set_scan_mode(st);
  928. return 0;
  929. }
  930. static const struct iio_info max1238_info = {
  931. .read_raw = &max1363_read_raw,
  932. .driver_module = THIS_MODULE,
  933. .update_scan_mode = &max1363_update_scan_mode,
  934. };
  935. static const struct iio_info max1363_info = {
  936. .read_event_value = &max1363_read_thresh,
  937. .write_event_value = &max1363_write_thresh,
  938. .read_event_config = &max1363_read_event_config,
  939. .write_event_config = &max1363_write_event_config,
  940. .read_raw = &max1363_read_raw,
  941. .update_scan_mode = &max1363_update_scan_mode,
  942. .driver_module = THIS_MODULE,
  943. .event_attrs = &max1363_event_attribute_group,
  944. };
  945. /* max1363 and max1368 tested - rest from data sheet */
  946. static const struct max1363_chip_info max1363_chip_info_tbl[] = {
  947. [max1361] = {
  948. .bits = 10,
  949. .int_vref_mv = 2048,
  950. .mode_list = max1363_mode_list,
  951. .num_modes = ARRAY_SIZE(max1363_mode_list),
  952. .default_mode = s0to3,
  953. .channels = max1361_channels,
  954. .num_channels = ARRAY_SIZE(max1361_channels),
  955. .info = &max1363_info,
  956. },
  957. [max1362] = {
  958. .bits = 10,
  959. .int_vref_mv = 4096,
  960. .mode_list = max1363_mode_list,
  961. .num_modes = ARRAY_SIZE(max1363_mode_list),
  962. .default_mode = s0to3,
  963. .channels = max1361_channels,
  964. .num_channels = ARRAY_SIZE(max1361_channels),
  965. .info = &max1363_info,
  966. },
  967. [max1363] = {
  968. .bits = 12,
  969. .int_vref_mv = 2048,
  970. .mode_list = max1363_mode_list,
  971. .num_modes = ARRAY_SIZE(max1363_mode_list),
  972. .default_mode = s0to3,
  973. .channels = max1363_channels,
  974. .num_channels = ARRAY_SIZE(max1363_channels),
  975. .info = &max1363_info,
  976. },
  977. [max1364] = {
  978. .bits = 12,
  979. .int_vref_mv = 4096,
  980. .mode_list = max1363_mode_list,
  981. .num_modes = ARRAY_SIZE(max1363_mode_list),
  982. .default_mode = s0to3,
  983. .channels = max1363_channels,
  984. .num_channels = ARRAY_SIZE(max1363_channels),
  985. .info = &max1363_info,
  986. },
  987. [max1036] = {
  988. .bits = 8,
  989. .int_vref_mv = 4096,
  990. .mode_list = max1236_mode_list,
  991. .num_modes = ARRAY_SIZE(max1236_mode_list),
  992. .default_mode = s0to3,
  993. .info = &max1238_info,
  994. .channels = max1036_channels,
  995. .num_channels = ARRAY_SIZE(max1036_channels),
  996. },
  997. [max1037] = {
  998. .bits = 8,
  999. .int_vref_mv = 2048,
  1000. .mode_list = max1236_mode_list,
  1001. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1002. .default_mode = s0to3,
  1003. .info = &max1238_info,
  1004. .channels = max1036_channels,
  1005. .num_channels = ARRAY_SIZE(max1036_channels),
  1006. },
  1007. [max1038] = {
  1008. .bits = 8,
  1009. .int_vref_mv = 4096,
  1010. .mode_list = max1238_mode_list,
  1011. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1012. .default_mode = s0to11,
  1013. .info = &max1238_info,
  1014. .channels = max1038_channels,
  1015. .num_channels = ARRAY_SIZE(max1038_channels),
  1016. },
  1017. [max1039] = {
  1018. .bits = 8,
  1019. .int_vref_mv = 2048,
  1020. .mode_list = max1238_mode_list,
  1021. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1022. .default_mode = s0to11,
  1023. .info = &max1238_info,
  1024. .channels = max1038_channels,
  1025. .num_channels = ARRAY_SIZE(max1038_channels),
  1026. },
  1027. [max1136] = {
  1028. .bits = 10,
  1029. .int_vref_mv = 4096,
  1030. .mode_list = max1236_mode_list,
  1031. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1032. .default_mode = s0to3,
  1033. .info = &max1238_info,
  1034. .channels = max1136_channels,
  1035. .num_channels = ARRAY_SIZE(max1136_channels),
  1036. },
  1037. [max1137] = {
  1038. .bits = 10,
  1039. .int_vref_mv = 2048,
  1040. .mode_list = max1236_mode_list,
  1041. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1042. .default_mode = s0to3,
  1043. .info = &max1238_info,
  1044. .channels = max1136_channels,
  1045. .num_channels = ARRAY_SIZE(max1136_channels),
  1046. },
  1047. [max1138] = {
  1048. .bits = 10,
  1049. .int_vref_mv = 4096,
  1050. .mode_list = max1238_mode_list,
  1051. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1052. .default_mode = s0to11,
  1053. .info = &max1238_info,
  1054. .channels = max1138_channels,
  1055. .num_channels = ARRAY_SIZE(max1138_channels),
  1056. },
  1057. [max1139] = {
  1058. .bits = 10,
  1059. .int_vref_mv = 2048,
  1060. .mode_list = max1238_mode_list,
  1061. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1062. .default_mode = s0to11,
  1063. .info = &max1238_info,
  1064. .channels = max1138_channels,
  1065. .num_channels = ARRAY_SIZE(max1138_channels),
  1066. },
  1067. [max1236] = {
  1068. .bits = 12,
  1069. .int_vref_mv = 4096,
  1070. .mode_list = max1236_mode_list,
  1071. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1072. .default_mode = s0to3,
  1073. .info = &max1238_info,
  1074. .channels = max1236_channels,
  1075. .num_channels = ARRAY_SIZE(max1236_channels),
  1076. },
  1077. [max1237] = {
  1078. .bits = 12,
  1079. .int_vref_mv = 2048,
  1080. .mode_list = max1236_mode_list,
  1081. .num_modes = ARRAY_SIZE(max1236_mode_list),
  1082. .default_mode = s0to3,
  1083. .info = &max1238_info,
  1084. .channels = max1236_channels,
  1085. .num_channels = ARRAY_SIZE(max1236_channels),
  1086. },
  1087. [max1238] = {
  1088. .bits = 12,
  1089. .int_vref_mv = 4096,
  1090. .mode_list = max1238_mode_list,
  1091. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1092. .default_mode = s0to11,
  1093. .info = &max1238_info,
  1094. .channels = max1238_channels,
  1095. .num_channels = ARRAY_SIZE(max1238_channels),
  1096. },
  1097. [max1239] = {
  1098. .bits = 12,
  1099. .int_vref_mv = 2048,
  1100. .mode_list = max1238_mode_list,
  1101. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1102. .default_mode = s0to11,
  1103. .info = &max1238_info,
  1104. .channels = max1238_channels,
  1105. .num_channels = ARRAY_SIZE(max1238_channels),
  1106. },
  1107. [max11600] = {
  1108. .bits = 8,
  1109. .int_vref_mv = 4096,
  1110. .mode_list = max11607_mode_list,
  1111. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1112. .default_mode = s0to3,
  1113. .info = &max1238_info,
  1114. .channels = max1036_channels,
  1115. .num_channels = ARRAY_SIZE(max1036_channels),
  1116. },
  1117. [max11601] = {
  1118. .bits = 8,
  1119. .int_vref_mv = 2048,
  1120. .mode_list = max11607_mode_list,
  1121. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1122. .default_mode = s0to3,
  1123. .info = &max1238_info,
  1124. .channels = max1036_channels,
  1125. .num_channels = ARRAY_SIZE(max1036_channels),
  1126. },
  1127. [max11602] = {
  1128. .bits = 8,
  1129. .int_vref_mv = 4096,
  1130. .mode_list = max11608_mode_list,
  1131. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1132. .default_mode = s0to7,
  1133. .info = &max1238_info,
  1134. .channels = max11602_channels,
  1135. .num_channels = ARRAY_SIZE(max11602_channels),
  1136. },
  1137. [max11603] = {
  1138. .bits = 8,
  1139. .int_vref_mv = 2048,
  1140. .mode_list = max11608_mode_list,
  1141. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1142. .default_mode = s0to7,
  1143. .info = &max1238_info,
  1144. .channels = max11602_channels,
  1145. .num_channels = ARRAY_SIZE(max11602_channels),
  1146. },
  1147. [max11604] = {
  1148. .bits = 8,
  1149. .int_vref_mv = 4096,
  1150. .mode_list = max1238_mode_list,
  1151. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1152. .default_mode = s0to11,
  1153. .info = &max1238_info,
  1154. .channels = max1038_channels,
  1155. .num_channels = ARRAY_SIZE(max1038_channels),
  1156. },
  1157. [max11605] = {
  1158. .bits = 8,
  1159. .int_vref_mv = 2048,
  1160. .mode_list = max1238_mode_list,
  1161. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1162. .default_mode = s0to11,
  1163. .info = &max1238_info,
  1164. .channels = max1038_channels,
  1165. .num_channels = ARRAY_SIZE(max1038_channels),
  1166. },
  1167. [max11606] = {
  1168. .bits = 10,
  1169. .int_vref_mv = 4096,
  1170. .mode_list = max11607_mode_list,
  1171. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1172. .default_mode = s0to3,
  1173. .info = &max1238_info,
  1174. .channels = max1136_channels,
  1175. .num_channels = ARRAY_SIZE(max1136_channels),
  1176. },
  1177. [max11607] = {
  1178. .bits = 10,
  1179. .int_vref_mv = 2048,
  1180. .mode_list = max11607_mode_list,
  1181. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1182. .default_mode = s0to3,
  1183. .info = &max1238_info,
  1184. .channels = max1136_channels,
  1185. .num_channels = ARRAY_SIZE(max1136_channels),
  1186. },
  1187. [max11608] = {
  1188. .bits = 10,
  1189. .int_vref_mv = 4096,
  1190. .mode_list = max11608_mode_list,
  1191. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1192. .default_mode = s0to7,
  1193. .info = &max1238_info,
  1194. .channels = max11608_channels,
  1195. .num_channels = ARRAY_SIZE(max11608_channels),
  1196. },
  1197. [max11609] = {
  1198. .bits = 10,
  1199. .int_vref_mv = 2048,
  1200. .mode_list = max11608_mode_list,
  1201. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1202. .default_mode = s0to7,
  1203. .info = &max1238_info,
  1204. .channels = max11608_channels,
  1205. .num_channels = ARRAY_SIZE(max11608_channels),
  1206. },
  1207. [max11610] = {
  1208. .bits = 10,
  1209. .int_vref_mv = 4096,
  1210. .mode_list = max1238_mode_list,
  1211. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1212. .default_mode = s0to11,
  1213. .info = &max1238_info,
  1214. .channels = max1138_channels,
  1215. .num_channels = ARRAY_SIZE(max1138_channels),
  1216. },
  1217. [max11611] = {
  1218. .bits = 10,
  1219. .int_vref_mv = 2048,
  1220. .mode_list = max1238_mode_list,
  1221. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1222. .default_mode = s0to11,
  1223. .info = &max1238_info,
  1224. .channels = max1138_channels,
  1225. .num_channels = ARRAY_SIZE(max1138_channels),
  1226. },
  1227. [max11612] = {
  1228. .bits = 12,
  1229. .int_vref_mv = 4096,
  1230. .mode_list = max11607_mode_list,
  1231. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1232. .default_mode = s0to3,
  1233. .info = &max1238_info,
  1234. .channels = max1363_channels,
  1235. .num_channels = ARRAY_SIZE(max1363_channels),
  1236. },
  1237. [max11613] = {
  1238. .bits = 12,
  1239. .int_vref_mv = 2048,
  1240. .mode_list = max11607_mode_list,
  1241. .num_modes = ARRAY_SIZE(max11607_mode_list),
  1242. .default_mode = s0to3,
  1243. .info = &max1238_info,
  1244. .channels = max1363_channels,
  1245. .num_channels = ARRAY_SIZE(max1363_channels),
  1246. },
  1247. [max11614] = {
  1248. .bits = 12,
  1249. .int_vref_mv = 4096,
  1250. .mode_list = max11608_mode_list,
  1251. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1252. .default_mode = s0to7,
  1253. .info = &max1238_info,
  1254. .channels = max11614_channels,
  1255. .num_channels = ARRAY_SIZE(max11614_channels),
  1256. },
  1257. [max11615] = {
  1258. .bits = 12,
  1259. .int_vref_mv = 2048,
  1260. .mode_list = max11608_mode_list,
  1261. .num_modes = ARRAY_SIZE(max11608_mode_list),
  1262. .default_mode = s0to7,
  1263. .info = &max1238_info,
  1264. .channels = max11614_channels,
  1265. .num_channels = ARRAY_SIZE(max11614_channels),
  1266. },
  1267. [max11616] = {
  1268. .bits = 12,
  1269. .int_vref_mv = 4096,
  1270. .mode_list = max1238_mode_list,
  1271. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1272. .default_mode = s0to11,
  1273. .info = &max1238_info,
  1274. .channels = max1238_channels,
  1275. .num_channels = ARRAY_SIZE(max1238_channels),
  1276. },
  1277. [max11617] = {
  1278. .bits = 12,
  1279. .int_vref_mv = 2048,
  1280. .mode_list = max1238_mode_list,
  1281. .num_modes = ARRAY_SIZE(max1238_mode_list),
  1282. .default_mode = s0to11,
  1283. .info = &max1238_info,
  1284. .channels = max1238_channels,
  1285. .num_channels = ARRAY_SIZE(max1238_channels),
  1286. },
  1287. [max11644] = {
  1288. .bits = 12,
  1289. .int_vref_mv = 4096,
  1290. .mode_list = max11644_mode_list,
  1291. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1292. .default_mode = s0to1,
  1293. .info = &max1238_info,
  1294. .channels = max11644_channels,
  1295. .num_channels = ARRAY_SIZE(max11644_channels),
  1296. },
  1297. [max11645] = {
  1298. .bits = 12,
  1299. .int_vref_mv = 2048,
  1300. .mode_list = max11644_mode_list,
  1301. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1302. .default_mode = s0to1,
  1303. .info = &max1238_info,
  1304. .channels = max11644_channels,
  1305. .num_channels = ARRAY_SIZE(max11644_channels),
  1306. },
  1307. [max11646] = {
  1308. .bits = 10,
  1309. .int_vref_mv = 4096,
  1310. .mode_list = max11644_mode_list,
  1311. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1312. .default_mode = s0to1,
  1313. .info = &max1238_info,
  1314. .channels = max11646_channels,
  1315. .num_channels = ARRAY_SIZE(max11646_channels),
  1316. },
  1317. [max11647] = {
  1318. .bits = 10,
  1319. .int_vref_mv = 2048,
  1320. .mode_list = max11644_mode_list,
  1321. .num_modes = ARRAY_SIZE(max11644_mode_list),
  1322. .default_mode = s0to1,
  1323. .info = &max1238_info,
  1324. .channels = max11646_channels,
  1325. .num_channels = ARRAY_SIZE(max11646_channels),
  1326. },
  1327. };
  1328. static int max1363_initial_setup(struct max1363_state *st)
  1329. {
  1330. st->setupbyte = MAX1363_SETUP_INT_CLOCK
  1331. | MAX1363_SETUP_UNIPOLAR
  1332. | MAX1363_SETUP_NORESET;
  1333. if (st->vref)
  1334. st->setupbyte |= MAX1363_SETUP_AIN3_IS_REF_EXT_TO_REF;
  1335. else
  1336. st->setupbyte |= MAX1363_SETUP_POWER_UP_INT_REF
  1337. | MAX1363_SETUP_AIN3_IS_AIN3_REF_IS_INT;
  1338. /* Set scan mode writes the config anyway so wait until then */
  1339. st->setupbyte = MAX1363_SETUP_BYTE(st->setupbyte);
  1340. st->current_mode = &max1363_mode_table[st->chip_info->default_mode];
  1341. st->configbyte = MAX1363_CONFIG_BYTE(st->configbyte);
  1342. return max1363_set_scan_mode(st);
  1343. }
  1344. static int max1363_alloc_scan_masks(struct iio_dev *indio_dev)
  1345. {
  1346. struct max1363_state *st = iio_priv(indio_dev);
  1347. unsigned long *masks;
  1348. int i;
  1349. masks = devm_kzalloc(&indio_dev->dev,
  1350. BITS_TO_LONGS(MAX1363_MAX_CHANNELS) * sizeof(long) *
  1351. (st->chip_info->num_modes + 1), GFP_KERNEL);
  1352. if (!masks)
  1353. return -ENOMEM;
  1354. for (i = 0; i < st->chip_info->num_modes; i++)
  1355. bitmap_copy(masks + BITS_TO_LONGS(MAX1363_MAX_CHANNELS)*i,
  1356. max1363_mode_table[st->chip_info->mode_list[i]]
  1357. .modemask, MAX1363_MAX_CHANNELS);
  1358. indio_dev->available_scan_masks = masks;
  1359. return 0;
  1360. }
  1361. static irqreturn_t max1363_trigger_handler(int irq, void *p)
  1362. {
  1363. struct iio_poll_func *pf = p;
  1364. struct iio_dev *indio_dev = pf->indio_dev;
  1365. struct max1363_state *st = iio_priv(indio_dev);
  1366. __u8 *rxbuf;
  1367. int b_sent;
  1368. size_t d_size;
  1369. unsigned long numvals = bitmap_weight(st->current_mode->modemask,
  1370. MAX1363_MAX_CHANNELS);
  1371. /* Ensure the timestamp is 8 byte aligned */
  1372. if (st->chip_info->bits != 8)
  1373. d_size = numvals*2;
  1374. else
  1375. d_size = numvals;
  1376. if (indio_dev->scan_timestamp) {
  1377. d_size += sizeof(s64);
  1378. if (d_size % sizeof(s64))
  1379. d_size += sizeof(s64) - (d_size % sizeof(s64));
  1380. }
  1381. /* Monitor mode prevents reading. Whilst not currently implemented
  1382. * might as well have this test in here in the meantime as it does
  1383. * no harm.
  1384. */
  1385. if (numvals == 0)
  1386. goto done;
  1387. rxbuf = kmalloc(d_size, GFP_KERNEL);
  1388. if (rxbuf == NULL)
  1389. goto done;
  1390. if (st->chip_info->bits != 8)
  1391. b_sent = st->recv(st->client, rxbuf, numvals * 2);
  1392. else
  1393. b_sent = st->recv(st->client, rxbuf, numvals);
  1394. if (b_sent < 0)
  1395. goto done_free;
  1396. iio_push_to_buffers_with_timestamp(indio_dev, rxbuf,
  1397. iio_get_time_ns(indio_dev));
  1398. done_free:
  1399. kfree(rxbuf);
  1400. done:
  1401. iio_trigger_notify_done(indio_dev->trig);
  1402. return IRQ_HANDLED;
  1403. }
  1404. #ifdef CONFIG_OF
  1405. #define MAX1363_COMPATIBLE(of_compatible, cfg) { \
  1406. .compatible = of_compatible, \
  1407. .data = &max1363_chip_info_tbl[cfg], \
  1408. }
  1409. static const struct of_device_id max1363_of_match[] = {
  1410. MAX1363_COMPATIBLE("maxim,max1361", max1361),
  1411. MAX1363_COMPATIBLE("maxim,max1362", max1362),
  1412. MAX1363_COMPATIBLE("maxim,max1363", max1363),
  1413. MAX1363_COMPATIBLE("maxim,max1364", max1364),
  1414. MAX1363_COMPATIBLE("maxim,max1036", max1036),
  1415. MAX1363_COMPATIBLE("maxim,max1037", max1037),
  1416. MAX1363_COMPATIBLE("maxim,max1038", max1038),
  1417. MAX1363_COMPATIBLE("maxim,max1039", max1039),
  1418. MAX1363_COMPATIBLE("maxim,max1136", max1136),
  1419. MAX1363_COMPATIBLE("maxim,max1137", max1137),
  1420. MAX1363_COMPATIBLE("maxim,max1138", max1138),
  1421. MAX1363_COMPATIBLE("maxim,max1139", max1139),
  1422. MAX1363_COMPATIBLE("maxim,max1236", max1236),
  1423. MAX1363_COMPATIBLE("maxim,max1237", max1237),
  1424. MAX1363_COMPATIBLE("maxim,max1238", max1238),
  1425. MAX1363_COMPATIBLE("maxim,max1239", max1239),
  1426. MAX1363_COMPATIBLE("maxim,max11600", max11600),
  1427. MAX1363_COMPATIBLE("maxim,max11601", max11601),
  1428. MAX1363_COMPATIBLE("maxim,max11602", max11602),
  1429. MAX1363_COMPATIBLE("maxim,max11603", max11603),
  1430. MAX1363_COMPATIBLE("maxim,max11604", max11604),
  1431. MAX1363_COMPATIBLE("maxim,max11605", max11605),
  1432. MAX1363_COMPATIBLE("maxim,max11606", max11606),
  1433. MAX1363_COMPATIBLE("maxim,max11607", max11607),
  1434. MAX1363_COMPATIBLE("maxim,max11608", max11608),
  1435. MAX1363_COMPATIBLE("maxim,max11609", max11609),
  1436. MAX1363_COMPATIBLE("maxim,max11610", max11610),
  1437. MAX1363_COMPATIBLE("maxim,max11611", max11611),
  1438. MAX1363_COMPATIBLE("maxim,max11612", max11612),
  1439. MAX1363_COMPATIBLE("maxim,max11613", max11613),
  1440. MAX1363_COMPATIBLE("maxim,max11614", max11614),
  1441. MAX1363_COMPATIBLE("maxim,max11615", max11615),
  1442. MAX1363_COMPATIBLE("maxim,max11616", max11616),
  1443. MAX1363_COMPATIBLE("maxim,max11617", max11617),
  1444. MAX1363_COMPATIBLE("maxim,max11644", max11644),
  1445. MAX1363_COMPATIBLE("maxim,max11645", max11645),
  1446. MAX1363_COMPATIBLE("maxim,max11646", max11646),
  1447. MAX1363_COMPATIBLE("maxim,max11647", max11647),
  1448. { /* sentinel */ }
  1449. };
  1450. #endif
  1451. static int max1363_probe(struct i2c_client *client,
  1452. const struct i2c_device_id *id)
  1453. {
  1454. int ret;
  1455. struct max1363_state *st;
  1456. struct iio_dev *indio_dev;
  1457. struct regulator *vref;
  1458. const struct of_device_id *match;
  1459. indio_dev = devm_iio_device_alloc(&client->dev,
  1460. sizeof(struct max1363_state));
  1461. if (!indio_dev)
  1462. return -ENOMEM;
  1463. indio_dev->dev.of_node = client->dev.of_node;
  1464. ret = iio_map_array_register(indio_dev, client->dev.platform_data);
  1465. if (ret < 0)
  1466. return ret;
  1467. st = iio_priv(indio_dev);
  1468. st->reg = devm_regulator_get(&client->dev, "vcc");
  1469. if (IS_ERR(st->reg)) {
  1470. ret = PTR_ERR(st->reg);
  1471. goto error_unregister_map;
  1472. }
  1473. ret = regulator_enable(st->reg);
  1474. if (ret)
  1475. goto error_unregister_map;
  1476. /* this is only used for device removal purposes */
  1477. i2c_set_clientdata(client, indio_dev);
  1478. match = of_match_device(of_match_ptr(max1363_of_match),
  1479. &client->dev);
  1480. if (match)
  1481. st->chip_info = of_device_get_match_data(&client->dev);
  1482. else
  1483. st->chip_info = &max1363_chip_info_tbl[id->driver_data];
  1484. st->client = client;
  1485. st->vref_uv = st->chip_info->int_vref_mv * 1000;
  1486. vref = devm_regulator_get_optional(&client->dev, "vref");
  1487. if (!IS_ERR(vref)) {
  1488. int vref_uv;
  1489. ret = regulator_enable(vref);
  1490. if (ret)
  1491. goto error_disable_reg;
  1492. st->vref = vref;
  1493. vref_uv = regulator_get_voltage(vref);
  1494. if (vref_uv <= 0) {
  1495. ret = -EINVAL;
  1496. goto error_disable_reg;
  1497. }
  1498. st->vref_uv = vref_uv;
  1499. }
  1500. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  1501. st->send = i2c_master_send;
  1502. st->recv = i2c_master_recv;
  1503. } else if (i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE)
  1504. && st->chip_info->bits == 8) {
  1505. st->send = max1363_smbus_send;
  1506. st->recv = max1363_smbus_recv;
  1507. } else {
  1508. ret = -EOPNOTSUPP;
  1509. goto error_disable_reg;
  1510. }
  1511. ret = max1363_alloc_scan_masks(indio_dev);
  1512. if (ret)
  1513. goto error_disable_reg;
  1514. /* Establish that the iio_dev is a child of the i2c device */
  1515. indio_dev->dev.parent = &client->dev;
  1516. indio_dev->dev.of_node = client->dev.of_node;
  1517. indio_dev->name = id->name;
  1518. indio_dev->channels = st->chip_info->channels;
  1519. indio_dev->num_channels = st->chip_info->num_channels;
  1520. indio_dev->info = st->chip_info->info;
  1521. indio_dev->modes = INDIO_DIRECT_MODE;
  1522. ret = max1363_initial_setup(st);
  1523. if (ret < 0)
  1524. goto error_disable_reg;
  1525. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  1526. &max1363_trigger_handler, NULL);
  1527. if (ret)
  1528. goto error_disable_reg;
  1529. if (client->irq) {
  1530. ret = devm_request_threaded_irq(&client->dev, st->client->irq,
  1531. NULL,
  1532. &max1363_event_handler,
  1533. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  1534. "max1363_event",
  1535. indio_dev);
  1536. if (ret)
  1537. goto error_uninit_buffer;
  1538. }
  1539. ret = iio_device_register(indio_dev);
  1540. if (ret < 0)
  1541. goto error_uninit_buffer;
  1542. return 0;
  1543. error_uninit_buffer:
  1544. iio_triggered_buffer_cleanup(indio_dev);
  1545. error_disable_reg:
  1546. if (st->vref)
  1547. regulator_disable(st->vref);
  1548. regulator_disable(st->reg);
  1549. error_unregister_map:
  1550. iio_map_array_unregister(indio_dev);
  1551. return ret;
  1552. }
  1553. static int max1363_remove(struct i2c_client *client)
  1554. {
  1555. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  1556. struct max1363_state *st = iio_priv(indio_dev);
  1557. iio_device_unregister(indio_dev);
  1558. iio_triggered_buffer_cleanup(indio_dev);
  1559. if (st->vref)
  1560. regulator_disable(st->vref);
  1561. regulator_disable(st->reg);
  1562. iio_map_array_unregister(indio_dev);
  1563. return 0;
  1564. }
  1565. static const struct i2c_device_id max1363_id[] = {
  1566. { "max1361", max1361 },
  1567. { "max1362", max1362 },
  1568. { "max1363", max1363 },
  1569. { "max1364", max1364 },
  1570. { "max1036", max1036 },
  1571. { "max1037", max1037 },
  1572. { "max1038", max1038 },
  1573. { "max1039", max1039 },
  1574. { "max1136", max1136 },
  1575. { "max1137", max1137 },
  1576. { "max1138", max1138 },
  1577. { "max1139", max1139 },
  1578. { "max1236", max1236 },
  1579. { "max1237", max1237 },
  1580. { "max1238", max1238 },
  1581. { "max1239", max1239 },
  1582. { "max11600", max11600 },
  1583. { "max11601", max11601 },
  1584. { "max11602", max11602 },
  1585. { "max11603", max11603 },
  1586. { "max11604", max11604 },
  1587. { "max11605", max11605 },
  1588. { "max11606", max11606 },
  1589. { "max11607", max11607 },
  1590. { "max11608", max11608 },
  1591. { "max11609", max11609 },
  1592. { "max11610", max11610 },
  1593. { "max11611", max11611 },
  1594. { "max11612", max11612 },
  1595. { "max11613", max11613 },
  1596. { "max11614", max11614 },
  1597. { "max11615", max11615 },
  1598. { "max11616", max11616 },
  1599. { "max11617", max11617 },
  1600. { "max11644", max11644 },
  1601. { "max11645", max11645 },
  1602. { "max11646", max11646 },
  1603. { "max11647", max11647 },
  1604. {}
  1605. };
  1606. MODULE_DEVICE_TABLE(i2c, max1363_id);
  1607. static struct i2c_driver max1363_driver = {
  1608. .driver = {
  1609. .name = "max1363",
  1610. .of_match_table = of_match_ptr(max1363_of_match),
  1611. },
  1612. .probe = max1363_probe,
  1613. .remove = max1363_remove,
  1614. .id_table = max1363_id,
  1615. };
  1616. module_i2c_driver(max1363_driver);
  1617. MODULE_AUTHOR("Jonathan Cameron <jic23@kernel.org>");
  1618. MODULE_DESCRIPTION("Maxim 1363 ADC");
  1619. MODULE_LICENSE("GPL v2");