max1363.c 47 KB

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