123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962 |
- /*
- * Atmel ADC driver for SAMA5D2 devices and compatible.
- *
- * Copyright (C) 2015 Atmel,
- * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
- *
- * This software is licensed under the terms of the GNU General Public
- * License version 2, as published by the Free Software Foundation, and
- * may be copied, distributed, and modified under those terms.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
- #include <linux/bitops.h>
- #include <linux/clk.h>
- #include <linux/dma-mapping.h>
- #include <linux/dmaengine.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/module.h>
- #include <linux/of_device.h>
- #include <linux/platform_device.h>
- #include <linux/sched.h>
- #include <linux/wait.h>
- #include <linux/iio/iio.h>
- #include <linux/iio/sysfs.h>
- #include <linux/iio/buffer.h>
- #include <linux/iio/trigger.h>
- #include <linux/iio/trigger_consumer.h>
- #include <linux/iio/triggered_buffer.h>
- #include <linux/pinctrl/consumer.h>
- #include <linux/regulator/consumer.h>
- /* Control Register */
- #define AT91_SAMA5D2_CR 0x00
- /* Software Reset */
- #define AT91_SAMA5D2_CR_SWRST BIT(0)
- /* Start Conversion */
- #define AT91_SAMA5D2_CR_START BIT(1)
- /* Touchscreen Calibration */
- #define AT91_SAMA5D2_CR_TSCALIB BIT(2)
- /* Comparison Restart */
- #define AT91_SAMA5D2_CR_CMPRST BIT(4)
- /* Mode Register */
- #define AT91_SAMA5D2_MR 0x04
- /* Trigger Selection */
- #define AT91_SAMA5D2_MR_TRGSEL(v) ((v) << 1)
- /* ADTRG */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG0 0
- /* TIOA0 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG1 1
- /* TIOA1 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG2 2
- /* TIOA2 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG3 3
- /* PWM event line 0 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG4 4
- /* PWM event line 1 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG5 5
- /* TIOA3 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG6 6
- /* RTCOUT0 */
- #define AT91_SAMA5D2_MR_TRGSEL_TRIG7 7
- /* Sleep Mode */
- #define AT91_SAMA5D2_MR_SLEEP BIT(5)
- /* Fast Wake Up */
- #define AT91_SAMA5D2_MR_FWUP BIT(6)
- /* Prescaler Rate Selection */
- #define AT91_SAMA5D2_MR_PRESCAL(v) ((v) << AT91_SAMA5D2_MR_PRESCAL_OFFSET)
- #define AT91_SAMA5D2_MR_PRESCAL_OFFSET 8
- #define AT91_SAMA5D2_MR_PRESCAL_MAX 0xff
- #define AT91_SAMA5D2_MR_PRESCAL_MASK GENMASK(15, 8)
- /* Startup Time */
- #define AT91_SAMA5D2_MR_STARTUP(v) ((v) << 16)
- #define AT91_SAMA5D2_MR_STARTUP_MASK GENMASK(19, 16)
- /* Analog Change */
- #define AT91_SAMA5D2_MR_ANACH BIT(23)
- /* Tracking Time */
- #define AT91_SAMA5D2_MR_TRACKTIM(v) ((v) << 24)
- #define AT91_SAMA5D2_MR_TRACKTIM_MAX 0xff
- /* Transfer Time */
- #define AT91_SAMA5D2_MR_TRANSFER(v) ((v) << 28)
- #define AT91_SAMA5D2_MR_TRANSFER_MAX 0x3
- /* Use Sequence Enable */
- #define AT91_SAMA5D2_MR_USEQ BIT(31)
- /* Channel Sequence Register 1 */
- #define AT91_SAMA5D2_SEQR1 0x08
- /* Channel Sequence Register 2 */
- #define AT91_SAMA5D2_SEQR2 0x0c
- /* Channel Enable Register */
- #define AT91_SAMA5D2_CHER 0x10
- /* Channel Disable Register */
- #define AT91_SAMA5D2_CHDR 0x14
- /* Channel Status Register */
- #define AT91_SAMA5D2_CHSR 0x18
- /* Last Converted Data Register */
- #define AT91_SAMA5D2_LCDR 0x20
- /* Interrupt Enable Register */
- #define AT91_SAMA5D2_IER 0x24
- /* Interrupt Enable Register - TS X measurement ready */
- #define AT91_SAMA5D2_IER_XRDY BIT(20)
- /* Interrupt Enable Register - TS Y measurement ready */
- #define AT91_SAMA5D2_IER_YRDY BIT(21)
- /* Interrupt Enable Register - TS pressure measurement ready */
- #define AT91_SAMA5D2_IER_PRDY BIT(22)
- /* Interrupt Enable Register - general overrun error */
- #define AT91_SAMA5D2_IER_GOVRE BIT(25)
- /* Interrupt Enable Register - Pen detect */
- #define AT91_SAMA5D2_IER_PEN BIT(29)
- /* Interrupt Enable Register - No pen detect */
- #define AT91_SAMA5D2_IER_NOPEN BIT(30)
- /* Interrupt Disable Register */
- #define AT91_SAMA5D2_IDR 0x28
- /* Interrupt Mask Register */
- #define AT91_SAMA5D2_IMR 0x2c
- /* Interrupt Status Register */
- #define AT91_SAMA5D2_ISR 0x30
- /* Interrupt Status Register - Pen touching sense status */
- #define AT91_SAMA5D2_ISR_PENS BIT(31)
- /* Last Channel Trigger Mode Register */
- #define AT91_SAMA5D2_LCTMR 0x34
- /* Last Channel Compare Window Register */
- #define AT91_SAMA5D2_LCCWR 0x38
- /* Overrun Status Register */
- #define AT91_SAMA5D2_OVER 0x3c
- /* Extended Mode Register */
- #define AT91_SAMA5D2_EMR 0x40
- /* Extended Mode Register - Oversampling rate */
- #define AT91_SAMA5D2_EMR_OSR(V) ((V) << 16)
- #define AT91_SAMA5D2_EMR_OSR_MASK GENMASK(17, 16)
- #define AT91_SAMA5D2_EMR_OSR_1SAMPLES 0
- #define AT91_SAMA5D2_EMR_OSR_4SAMPLES 1
- #define AT91_SAMA5D2_EMR_OSR_16SAMPLES 2
- /* Extended Mode Register - Averaging on single trigger event */
- #define AT91_SAMA5D2_EMR_ASTE(V) ((V) << 20)
- /* Compare Window Register */
- #define AT91_SAMA5D2_CWR 0x44
- /* Channel Gain Register */
- #define AT91_SAMA5D2_CGR 0x48
- /* Channel Offset Register */
- #define AT91_SAMA5D2_COR 0x4c
- #define AT91_SAMA5D2_COR_DIFF_OFFSET 16
- /* Channel Data Register 0 */
- #define AT91_SAMA5D2_CDR0 0x50
- /* Analog Control Register */
- #define AT91_SAMA5D2_ACR 0x94
- /* Analog Control Register - Pen detect sensitivity mask */
- #define AT91_SAMA5D2_ACR_PENDETSENS_MASK GENMASK(1, 0)
- /* Touchscreen Mode Register */
- #define AT91_SAMA5D2_TSMR 0xb0
- /* Touchscreen Mode Register - No touch mode */
- #define AT91_SAMA5D2_TSMR_TSMODE_NONE 0
- /* Touchscreen Mode Register - 4 wire screen, no pressure measurement */
- #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_NO_PRESS 1
- /* Touchscreen Mode Register - 4 wire screen, pressure measurement */
- #define AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS 2
- /* Touchscreen Mode Register - 5 wire screen */
- #define AT91_SAMA5D2_TSMR_TSMODE_5WIRE 3
- /* Touchscreen Mode Register - Average samples mask */
- #define AT91_SAMA5D2_TSMR_TSAV_MASK GENMASK(5, 4)
- /* Touchscreen Mode Register - Average samples */
- #define AT91_SAMA5D2_TSMR_TSAV(x) ((x) << 4)
- /* Touchscreen Mode Register - Touch/trigger frequency ratio mask */
- #define AT91_SAMA5D2_TSMR_TSFREQ_MASK GENMASK(11, 8)
- /* Touchscreen Mode Register - Touch/trigger frequency ratio */
- #define AT91_SAMA5D2_TSMR_TSFREQ(x) ((x) << 8)
- /* Touchscreen Mode Register - Pen Debounce Time mask */
- #define AT91_SAMA5D2_TSMR_PENDBC_MASK GENMASK(31, 28)
- /* Touchscreen Mode Register - Pen Debounce Time */
- #define AT91_SAMA5D2_TSMR_PENDBC(x) ((x) << 28)
- /* Touchscreen Mode Register - No DMA for touch measurements */
- #define AT91_SAMA5D2_TSMR_NOTSDMA BIT(22)
- /* Touchscreen Mode Register - Disable pen detection */
- #define AT91_SAMA5D2_TSMR_PENDET_DIS (0 << 24)
- /* Touchscreen Mode Register - Enable pen detection */
- #define AT91_SAMA5D2_TSMR_PENDET_ENA BIT(24)
- /* Touchscreen X Position Register */
- #define AT91_SAMA5D2_XPOSR 0xb4
- /* Touchscreen Y Position Register */
- #define AT91_SAMA5D2_YPOSR 0xb8
- /* Touchscreen Pressure Register */
- #define AT91_SAMA5D2_PRESSR 0xbc
- /* Trigger Register */
- #define AT91_SAMA5D2_TRGR 0xc0
- /* Mask for TRGMOD field of TRGR register */
- #define AT91_SAMA5D2_TRGR_TRGMOD_MASK GENMASK(2, 0)
- /* No trigger, only software trigger can start conversions */
- #define AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER 0
- /* Trigger Mode external trigger rising edge */
- #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE 1
- /* Trigger Mode external trigger falling edge */
- #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL 2
- /* Trigger Mode external trigger any edge */
- #define AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY 3
- /* Trigger Mode internal periodic */
- #define AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC 5
- /* Trigger Mode - trigger period mask */
- #define AT91_SAMA5D2_TRGR_TRGPER_MASK GENMASK(31, 16)
- /* Trigger Mode - trigger period */
- #define AT91_SAMA5D2_TRGR_TRGPER(x) ((x) << 16)
- /* Correction Select Register */
- #define AT91_SAMA5D2_COSR 0xd0
- /* Correction Value Register */
- #define AT91_SAMA5D2_CVR 0xd4
- /* Channel Error Correction Register */
- #define AT91_SAMA5D2_CECR 0xd8
- /* Write Protection Mode Register */
- #define AT91_SAMA5D2_WPMR 0xe4
- /* Write Protection Status Register */
- #define AT91_SAMA5D2_WPSR 0xe8
- /* Version Register */
- #define AT91_SAMA5D2_VERSION 0xfc
- #define AT91_SAMA5D2_HW_TRIG_CNT 3
- #define AT91_SAMA5D2_SINGLE_CHAN_CNT 12
- #define AT91_SAMA5D2_DIFF_CHAN_CNT 6
- #define AT91_SAMA5D2_TIMESTAMP_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
- AT91_SAMA5D2_DIFF_CHAN_CNT + 1)
- #define AT91_SAMA5D2_TOUCH_X_CHAN_IDX (AT91_SAMA5D2_SINGLE_CHAN_CNT + \
- AT91_SAMA5D2_DIFF_CHAN_CNT * 2)
- #define AT91_SAMA5D2_TOUCH_Y_CHAN_IDX (AT91_SAMA5D2_TOUCH_X_CHAN_IDX + 1)
- #define AT91_SAMA5D2_TOUCH_P_CHAN_IDX (AT91_SAMA5D2_TOUCH_Y_CHAN_IDX + 1)
- #define AT91_SAMA5D2_MAX_CHAN_IDX AT91_SAMA5D2_TOUCH_P_CHAN_IDX
- #define AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US 2000 /* 2ms */
- #define AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US 200
- #define AT91_SAMA5D2_XYZ_MASK GENMASK(11, 0)
- #define AT91_SAMA5D2_MAX_POS_BITS 12
- /*
- * Maximum number of bytes to hold conversion from all channels
- * without the timestamp.
- */
- #define AT91_BUFFER_MAX_CONVERSION_BYTES ((AT91_SAMA5D2_SINGLE_CHAN_CNT + \
- AT91_SAMA5D2_DIFF_CHAN_CNT) * 2)
- /* This total must also include the timestamp */
- #define AT91_BUFFER_MAX_BYTES (AT91_BUFFER_MAX_CONVERSION_BYTES + 8)
- #define AT91_BUFFER_MAX_HWORDS (AT91_BUFFER_MAX_BYTES / 2)
- #define AT91_HWFIFO_MAX_SIZE_STR "128"
- #define AT91_HWFIFO_MAX_SIZE 128
- /* Possible values for oversampling ratio */
- #define AT91_OSR_1SAMPLES 1
- #define AT91_OSR_4SAMPLES 4
- #define AT91_OSR_16SAMPLES 16
- #define AT91_SAMA5D2_CHAN_SINGLE(num, addr) \
- { \
- .type = IIO_VOLTAGE, \
- .channel = num, \
- .address = addr, \
- .scan_index = num, \
- .scan_type = { \
- .sign = 'u', \
- .realbits = 14, \
- .storagebits = 16, \
- }, \
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
- .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
- BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
- .datasheet_name = "CH"#num, \
- .indexed = 1, \
- }
- #define AT91_SAMA5D2_CHAN_DIFF(num, num2, addr) \
- { \
- .type = IIO_VOLTAGE, \
- .differential = 1, \
- .channel = num, \
- .channel2 = num2, \
- .address = addr, \
- .scan_index = num + AT91_SAMA5D2_SINGLE_CHAN_CNT, \
- .scan_type = { \
- .sign = 's', \
- .realbits = 14, \
- .storagebits = 16, \
- }, \
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
- .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
- .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
- BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
- .datasheet_name = "CH"#num"-CH"#num2, \
- .indexed = 1, \
- }
- #define AT91_SAMA5D2_CHAN_TOUCH(num, name, mod) \
- { \
- .type = IIO_POSITIONRELATIVE, \
- .modified = 1, \
- .channel = num, \
- .channel2 = mod, \
- .scan_index = num, \
- .scan_type = { \
- .sign = 'u', \
- .realbits = 12, \
- .storagebits = 16, \
- }, \
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
- .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
- BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
- .datasheet_name = name, \
- }
- #define AT91_SAMA5D2_CHAN_PRESSURE(num, name) \
- { \
- .type = IIO_PRESSURE, \
- .channel = num, \
- .scan_index = num, \
- .scan_type = { \
- .sign = 'u', \
- .realbits = 12, \
- .storagebits = 16, \
- }, \
- .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
- .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ)|\
- BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
- .datasheet_name = name, \
- }
- #define at91_adc_readl(st, reg) readl_relaxed(st->base + reg)
- #define at91_adc_writel(st, reg, val) writel_relaxed(val, st->base + reg)
- struct at91_adc_soc_info {
- unsigned startup_time;
- unsigned min_sample_rate;
- unsigned max_sample_rate;
- };
- struct at91_adc_trigger {
- char *name;
- unsigned int trgmod_value;
- unsigned int edge_type;
- bool hw_trig;
- };
- /**
- * at91_adc_dma - at91-sama5d2 dma information struct
- * @dma_chan: the dma channel acquired
- * @rx_buf: dma coherent allocated area
- * @rx_dma_buf: dma handler for the buffer
- * @phys_addr: physical address of the ADC base register
- * @buf_idx: index inside the dma buffer where reading was last done
- * @rx_buf_sz: size of buffer used by DMA operation
- * @watermark: number of conversions to copy before DMA triggers irq
- * @dma_ts: hold the start timestamp of dma operation
- */
- struct at91_adc_dma {
- struct dma_chan *dma_chan;
- u8 *rx_buf;
- dma_addr_t rx_dma_buf;
- phys_addr_t phys_addr;
- int buf_idx;
- int rx_buf_sz;
- int watermark;
- s64 dma_ts;
- };
- /**
- * at91_adc_touch - at91-sama5d2 touchscreen information struct
- * @sample_period_val: the value for periodic trigger interval
- * @touching: is the pen touching the screen or not
- * @x_pos: temporary placeholder for pressure computation
- * @channels_bitmask: bitmask with the touchscreen channels enabled
- * @workq: workqueue for buffer data pushing
- */
- struct at91_adc_touch {
- u16 sample_period_val;
- bool touching;
- u16 x_pos;
- unsigned long channels_bitmask;
- struct work_struct workq;
- };
- struct at91_adc_state {
- void __iomem *base;
- int irq;
- struct clk *per_clk;
- struct regulator *reg;
- struct regulator *vref;
- int vref_uv;
- unsigned int current_sample_rate;
- struct iio_trigger *trig;
- const struct at91_adc_trigger *selected_trig;
- const struct iio_chan_spec *chan;
- bool conversion_done;
- u32 conversion_value;
- unsigned int oversampling_ratio;
- struct at91_adc_soc_info soc_info;
- wait_queue_head_t wq_data_available;
- struct at91_adc_dma dma_st;
- struct at91_adc_touch touch_st;
- u16 buffer[AT91_BUFFER_MAX_HWORDS];
- /*
- * lock to prevent concurrent 'single conversion' requests through
- * sysfs.
- */
- struct mutex lock;
- };
- static const struct at91_adc_trigger at91_adc_trigger_list[] = {
- {
- .name = "external_rising",
- .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_RISE,
- .edge_type = IRQ_TYPE_EDGE_RISING,
- .hw_trig = true,
- },
- {
- .name = "external_falling",
- .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_FALL,
- .edge_type = IRQ_TYPE_EDGE_FALLING,
- .hw_trig = true,
- },
- {
- .name = "external_any",
- .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_EXT_TRIG_ANY,
- .edge_type = IRQ_TYPE_EDGE_BOTH,
- .hw_trig = true,
- },
- {
- .name = "software",
- .trgmod_value = AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER,
- .edge_type = IRQ_TYPE_NONE,
- .hw_trig = false,
- },
- };
- static const struct iio_chan_spec at91_adc_channels[] = {
- AT91_SAMA5D2_CHAN_SINGLE(0, 0x50),
- AT91_SAMA5D2_CHAN_SINGLE(1, 0x54),
- AT91_SAMA5D2_CHAN_SINGLE(2, 0x58),
- AT91_SAMA5D2_CHAN_SINGLE(3, 0x5c),
- AT91_SAMA5D2_CHAN_SINGLE(4, 0x60),
- AT91_SAMA5D2_CHAN_SINGLE(5, 0x64),
- AT91_SAMA5D2_CHAN_SINGLE(6, 0x68),
- AT91_SAMA5D2_CHAN_SINGLE(7, 0x6c),
- AT91_SAMA5D2_CHAN_SINGLE(8, 0x70),
- AT91_SAMA5D2_CHAN_SINGLE(9, 0x74),
- AT91_SAMA5D2_CHAN_SINGLE(10, 0x78),
- AT91_SAMA5D2_CHAN_SINGLE(11, 0x7c),
- AT91_SAMA5D2_CHAN_DIFF(0, 1, 0x50),
- AT91_SAMA5D2_CHAN_DIFF(2, 3, 0x58),
- AT91_SAMA5D2_CHAN_DIFF(4, 5, 0x60),
- AT91_SAMA5D2_CHAN_DIFF(6, 7, 0x68),
- AT91_SAMA5D2_CHAN_DIFF(8, 9, 0x70),
- AT91_SAMA5D2_CHAN_DIFF(10, 11, 0x78),
- IIO_CHAN_SOFT_TIMESTAMP(AT91_SAMA5D2_TIMESTAMP_CHAN_IDX),
- AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_X_CHAN_IDX, "x", IIO_MOD_X),
- AT91_SAMA5D2_CHAN_TOUCH(AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, "y", IIO_MOD_Y),
- AT91_SAMA5D2_CHAN_PRESSURE(AT91_SAMA5D2_TOUCH_P_CHAN_IDX, "pressure"),
- };
- static int at91_adc_chan_xlate(struct iio_dev *indio_dev, int chan)
- {
- int i;
- for (i = 0; i < indio_dev->num_channels; i++) {
- if (indio_dev->channels[i].scan_index == chan)
- return i;
- }
- return -EINVAL;
- }
- static inline struct iio_chan_spec const *
- at91_adc_chan_get(struct iio_dev *indio_dev, int chan)
- {
- int index = at91_adc_chan_xlate(indio_dev, chan);
- if (index < 0)
- return NULL;
- return indio_dev->channels + index;
- }
- static inline int at91_adc_of_xlate(struct iio_dev *indio_dev,
- const struct of_phandle_args *iiospec)
- {
- return at91_adc_chan_xlate(indio_dev, iiospec->args[0]);
- }
- static void at91_adc_config_emr(struct at91_adc_state *st)
- {
- /* configure the extended mode register */
- unsigned int emr = at91_adc_readl(st, AT91_SAMA5D2_EMR);
- /* select oversampling per single trigger event */
- emr |= AT91_SAMA5D2_EMR_ASTE(1);
- /* delete leftover content if it's the case */
- emr &= ~AT91_SAMA5D2_EMR_OSR_MASK;
- /* select oversampling ratio from configuration */
- switch (st->oversampling_ratio) {
- case AT91_OSR_1SAMPLES:
- emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_1SAMPLES) &
- AT91_SAMA5D2_EMR_OSR_MASK;
- break;
- case AT91_OSR_4SAMPLES:
- emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_4SAMPLES) &
- AT91_SAMA5D2_EMR_OSR_MASK;
- break;
- case AT91_OSR_16SAMPLES:
- emr |= AT91_SAMA5D2_EMR_OSR(AT91_SAMA5D2_EMR_OSR_16SAMPLES) &
- AT91_SAMA5D2_EMR_OSR_MASK;
- break;
- }
- at91_adc_writel(st, AT91_SAMA5D2_EMR, emr);
- }
- static int at91_adc_adjust_val_osr(struct at91_adc_state *st, int *val)
- {
- if (st->oversampling_ratio == AT91_OSR_1SAMPLES) {
- /*
- * in this case we only have 12 bits of real data, but channel
- * is registered as 14 bits, so shift left two bits
- */
- *val <<= 2;
- } else if (st->oversampling_ratio == AT91_OSR_4SAMPLES) {
- /*
- * in this case we have 13 bits of real data, but channel
- * is registered as 14 bits, so left shift one bit
- */
- *val <<= 1;
- }
- return IIO_VAL_INT;
- }
- static void at91_adc_adjust_val_osr_array(struct at91_adc_state *st, void *buf,
- int len)
- {
- int i = 0, val;
- u16 *buf_u16 = (u16 *) buf;
- /*
- * We are converting each two bytes (each sample).
- * First convert the byte based array to u16, and convert each sample
- * separately.
- * Each value is two bytes in an array of chars, so to not shift
- * more than we need, save the value separately.
- * len is in bytes, so divide by two to get number of samples.
- */
- while (i < len / 2) {
- val = buf_u16[i];
- at91_adc_adjust_val_osr(st, &val);
- buf_u16[i] = val;
- i++;
- }
- }
- static int at91_adc_configure_touch(struct at91_adc_state *st, bool state)
- {
- u32 clk_khz = st->current_sample_rate / 1000;
- int i = 0;
- u16 pendbc;
- u32 tsmr, acr;
- if (!state) {
- /* disabling touch IRQs and setting mode to no touch enabled */
- at91_adc_writel(st, AT91_SAMA5D2_IDR,
- AT91_SAMA5D2_IER_PEN | AT91_SAMA5D2_IER_NOPEN);
- at91_adc_writel(st, AT91_SAMA5D2_TSMR, 0);
- return 0;
- }
- /*
- * debounce time is in microseconds, we need it in milliseconds to
- * multiply with kilohertz, so, divide by 1000, but after the multiply.
- * round up to make sure pendbc is at least 1
- */
- pendbc = round_up(AT91_SAMA5D2_TOUCH_PEN_DETECT_DEBOUNCE_US *
- clk_khz / 1000, 1);
- /* get the required exponent */
- while (pendbc >> i++)
- ;
- pendbc = i;
- tsmr = AT91_SAMA5D2_TSMR_TSMODE_4WIRE_PRESS;
- tsmr |= AT91_SAMA5D2_TSMR_TSAV(2) & AT91_SAMA5D2_TSMR_TSAV_MASK;
- tsmr |= AT91_SAMA5D2_TSMR_PENDBC(pendbc) &
- AT91_SAMA5D2_TSMR_PENDBC_MASK;
- tsmr |= AT91_SAMA5D2_TSMR_NOTSDMA;
- tsmr |= AT91_SAMA5D2_TSMR_PENDET_ENA;
- tsmr |= AT91_SAMA5D2_TSMR_TSFREQ(2) & AT91_SAMA5D2_TSMR_TSFREQ_MASK;
- at91_adc_writel(st, AT91_SAMA5D2_TSMR, tsmr);
- acr = at91_adc_readl(st, AT91_SAMA5D2_ACR);
- acr &= ~AT91_SAMA5D2_ACR_PENDETSENS_MASK;
- acr |= 0x02 & AT91_SAMA5D2_ACR_PENDETSENS_MASK;
- at91_adc_writel(st, AT91_SAMA5D2_ACR, acr);
- /* Sample Period Time = (TRGPER + 1) / ADCClock */
- st->touch_st.sample_period_val =
- round_up((AT91_SAMA5D2_TOUCH_SAMPLE_PERIOD_US *
- clk_khz / 1000) - 1, 1);
- /* enable pen detect IRQ */
- at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN);
- return 0;
- }
- static u16 at91_adc_touch_pos(struct at91_adc_state *st, int reg)
- {
- u32 val;
- u32 scale, result, pos;
- /*
- * to obtain the actual position we must divide by scale
- * and multiply with max, where
- * max = 2^AT91_SAMA5D2_MAX_POS_BITS - 1
- */
- /* first half of register is the x or y, second half is the scale */
- val = at91_adc_readl(st, reg);
- if (!val)
- dev_dbg(&iio_priv_to_dev(st)->dev, "pos is 0\n");
- pos = val & AT91_SAMA5D2_XYZ_MASK;
- result = (pos << AT91_SAMA5D2_MAX_POS_BITS) - pos;
- scale = (val >> 16) & AT91_SAMA5D2_XYZ_MASK;
- if (scale == 0) {
- dev_err(&iio_priv_to_dev(st)->dev, "scale is 0\n");
- return 0;
- }
- result /= scale;
- return result;
- }
- static u16 at91_adc_touch_x_pos(struct at91_adc_state *st)
- {
- st->touch_st.x_pos = at91_adc_touch_pos(st, AT91_SAMA5D2_XPOSR);
- return st->touch_st.x_pos;
- }
- static u16 at91_adc_touch_y_pos(struct at91_adc_state *st)
- {
- return at91_adc_touch_pos(st, AT91_SAMA5D2_YPOSR);
- }
- static u16 at91_adc_touch_pressure(struct at91_adc_state *st)
- {
- u32 val;
- u32 z1, z2;
- u32 pres;
- u32 rxp = 1;
- u32 factor = 1000;
- /* calculate the pressure */
- val = at91_adc_readl(st, AT91_SAMA5D2_PRESSR);
- z1 = val & AT91_SAMA5D2_XYZ_MASK;
- z2 = (val >> 16) & AT91_SAMA5D2_XYZ_MASK;
- if (z1 != 0)
- pres = rxp * (st->touch_st.x_pos * factor / 1024) *
- (z2 * factor / z1 - factor) /
- factor;
- else
- pres = 0xFFFF; /* no pen contact */
- /*
- * The pressure from device grows down, minimum is 0xFFFF, maximum 0x0.
- * We compute it this way, but let's return it in the expected way,
- * growing from 0 to 0xFFFF.
- */
- return 0xFFFF - pres;
- }
- static int at91_adc_read_position(struct at91_adc_state *st, int chan, u16 *val)
- {
- *val = 0;
- if (!st->touch_st.touching)
- return -ENODATA;
- if (chan == AT91_SAMA5D2_TOUCH_X_CHAN_IDX)
- *val = at91_adc_touch_x_pos(st);
- else if (chan == AT91_SAMA5D2_TOUCH_Y_CHAN_IDX)
- *val = at91_adc_touch_y_pos(st);
- else
- return -ENODATA;
- return IIO_VAL_INT;
- }
- static int at91_adc_read_pressure(struct at91_adc_state *st, int chan, u16 *val)
- {
- *val = 0;
- if (!st->touch_st.touching)
- return -ENODATA;
- if (chan == AT91_SAMA5D2_TOUCH_P_CHAN_IDX)
- *val = at91_adc_touch_pressure(st);
- else
- return -ENODATA;
- return IIO_VAL_INT;
- }
- static int at91_adc_configure_trigger(struct iio_trigger *trig, bool state)
- {
- struct iio_dev *indio = iio_trigger_get_drvdata(trig);
- struct at91_adc_state *st = iio_priv(indio);
- u32 status = at91_adc_readl(st, AT91_SAMA5D2_TRGR);
- u8 bit;
- /* clear TRGMOD */
- status &= ~AT91_SAMA5D2_TRGR_TRGMOD_MASK;
- if (state)
- status |= st->selected_trig->trgmod_value;
- /* set/unset hw trigger */
- at91_adc_writel(st, AT91_SAMA5D2_TRGR, status);
- for_each_set_bit(bit, indio->active_scan_mask, indio->num_channels) {
- struct iio_chan_spec const *chan = at91_adc_chan_get(indio, bit);
- u32 cor;
- if (!chan)
- continue;
- /* these channel types cannot be handled by this trigger */
- if (chan->type == IIO_POSITIONRELATIVE ||
- chan->type == IIO_PRESSURE)
- continue;
- if (state) {
- cor = at91_adc_readl(st, AT91_SAMA5D2_COR);
- if (chan->differential)
- cor |= (BIT(chan->channel) |
- BIT(chan->channel2)) <<
- AT91_SAMA5D2_COR_DIFF_OFFSET;
- else
- cor &= ~(BIT(chan->channel) <<
- AT91_SAMA5D2_COR_DIFF_OFFSET);
- at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
- }
- if (state) {
- at91_adc_writel(st, AT91_SAMA5D2_CHER,
- BIT(chan->channel));
- /* enable irq only if not using DMA */
- if (!st->dma_st.dma_chan) {
- at91_adc_writel(st, AT91_SAMA5D2_IER,
- BIT(chan->channel));
- }
- } else {
- /* disable irq only if not using DMA */
- if (!st->dma_st.dma_chan) {
- at91_adc_writel(st, AT91_SAMA5D2_IDR,
- BIT(chan->channel));
- }
- at91_adc_writel(st, AT91_SAMA5D2_CHDR,
- BIT(chan->channel));
- }
- }
- return 0;
- }
- static int at91_adc_reenable_trigger(struct iio_trigger *trig)
- {
- struct iio_dev *indio = iio_trigger_get_drvdata(trig);
- struct at91_adc_state *st = iio_priv(indio);
- /* if we are using DMA, we must not reenable irq after each trigger */
- if (st->dma_st.dma_chan)
- return 0;
- enable_irq(st->irq);
- /* Needed to ACK the DRDY interruption */
- at91_adc_readl(st, AT91_SAMA5D2_LCDR);
- return 0;
- }
- static const struct iio_trigger_ops at91_adc_trigger_ops = {
- .set_trigger_state = &at91_adc_configure_trigger,
- .try_reenable = &at91_adc_reenable_trigger,
- .validate_device = iio_trigger_validate_own_device,
- };
- static int at91_adc_dma_size_done(struct at91_adc_state *st)
- {
- struct dma_tx_state state;
- enum dma_status status;
- int i, size;
- status = dmaengine_tx_status(st->dma_st.dma_chan,
- st->dma_st.dma_chan->cookie,
- &state);
- if (status != DMA_IN_PROGRESS)
- return 0;
- /* Transferred length is size in bytes from end of buffer */
- i = st->dma_st.rx_buf_sz - state.residue;
- /* Return available bytes */
- if (i >= st->dma_st.buf_idx)
- size = i - st->dma_st.buf_idx;
- else
- size = st->dma_st.rx_buf_sz + i - st->dma_st.buf_idx;
- return size;
- }
- static void at91_dma_buffer_done(void *data)
- {
- struct iio_dev *indio_dev = data;
- iio_trigger_poll_chained(indio_dev->trig);
- }
- static int at91_adc_dma_start(struct iio_dev *indio_dev)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- struct dma_async_tx_descriptor *desc;
- dma_cookie_t cookie;
- int ret;
- u8 bit;
- if (!st->dma_st.dma_chan)
- return 0;
- /* we start a new DMA, so set buffer index to start */
- st->dma_st.buf_idx = 0;
- /*
- * compute buffer size w.r.t. watermark and enabled channels.
- * scan_bytes is aligned so we need an exact size for DMA
- */
- st->dma_st.rx_buf_sz = 0;
- for_each_set_bit(bit, indio_dev->active_scan_mask,
- indio_dev->num_channels) {
- struct iio_chan_spec const *chan =
- at91_adc_chan_get(indio_dev, bit);
- if (!chan)
- continue;
- st->dma_st.rx_buf_sz += chan->scan_type.storagebits / 8;
- }
- st->dma_st.rx_buf_sz *= st->dma_st.watermark;
- /* Prepare a DMA cyclic transaction */
- desc = dmaengine_prep_dma_cyclic(st->dma_st.dma_chan,
- st->dma_st.rx_dma_buf,
- st->dma_st.rx_buf_sz,
- st->dma_st.rx_buf_sz / 2,
- DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
- if (!desc) {
- dev_err(&indio_dev->dev, "cannot prepare DMA cyclic\n");
- return -EBUSY;
- }
- desc->callback = at91_dma_buffer_done;
- desc->callback_param = indio_dev;
- cookie = dmaengine_submit(desc);
- ret = dma_submit_error(cookie);
- if (ret) {
- dev_err(&indio_dev->dev, "cannot submit DMA cyclic\n");
- dmaengine_terminate_async(st->dma_st.dma_chan);
- return ret;
- }
- /* enable general overrun error signaling */
- at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_GOVRE);
- /* Issue pending DMA requests */
- dma_async_issue_pending(st->dma_st.dma_chan);
- /* consider current time as DMA start time for timestamps */
- st->dma_st.dma_ts = iio_get_time_ns(indio_dev);
- dev_dbg(&indio_dev->dev, "DMA cyclic started\n");
- return 0;
- }
- static int at91_adc_buffer_postenable(struct iio_dev *indio_dev)
- {
- int ret;
- struct at91_adc_state *st = iio_priv(indio_dev);
- /* check if we are enabling triggered buffer or the touchscreen */
- if (bitmap_subset(indio_dev->active_scan_mask,
- &st->touch_st.channels_bitmask,
- AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
- /* touchscreen enabling */
- return at91_adc_configure_touch(st, true);
- }
- /* if we are not in triggered mode, we cannot enable the buffer. */
- if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES))
- return -EINVAL;
- /* we continue with the triggered buffer */
- ret = at91_adc_dma_start(indio_dev);
- if (ret) {
- dev_err(&indio_dev->dev, "buffer postenable failed\n");
- return ret;
- }
- return iio_triggered_buffer_postenable(indio_dev);
- }
- static int at91_adc_buffer_predisable(struct iio_dev *indio_dev)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- int ret;
- u8 bit;
- /* check if we are disabling triggered buffer or the touchscreen */
- if (bitmap_subset(indio_dev->active_scan_mask,
- &st->touch_st.channels_bitmask,
- AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
- /* touchscreen disable */
- return at91_adc_configure_touch(st, false);
- }
- /* if we are not in triggered mode, nothing to do here */
- if (!(indio_dev->currentmode & INDIO_ALL_TRIGGERED_MODES))
- return -EINVAL;
- /* continue with the triggered buffer */
- ret = iio_triggered_buffer_predisable(indio_dev);
- if (ret < 0)
- dev_err(&indio_dev->dev, "buffer predisable failed\n");
- if (!st->dma_st.dma_chan)
- return ret;
- /* if we are using DMA we must clear registers and end DMA */
- dmaengine_terminate_sync(st->dma_st.dma_chan);
- /*
- * For each enabled channel we must read the last converted value
- * to clear EOC status and not get a possible interrupt later.
- * This value is being read by DMA from LCDR anyway
- */
- for_each_set_bit(bit, indio_dev->active_scan_mask,
- indio_dev->num_channels) {
- struct iio_chan_spec const *chan =
- at91_adc_chan_get(indio_dev, bit);
- if (!chan)
- continue;
- /* these channel types are virtual, no need to do anything */
- if (chan->type == IIO_POSITIONRELATIVE ||
- chan->type == IIO_PRESSURE)
- continue;
- if (st->dma_st.dma_chan)
- at91_adc_readl(st, chan->address);
- }
- /* read overflow register to clear possible overflow status */
- at91_adc_readl(st, AT91_SAMA5D2_OVER);
- return ret;
- }
- static const struct iio_buffer_setup_ops at91_buffer_setup_ops = {
- .postenable = &at91_adc_buffer_postenable,
- .predisable = &at91_adc_buffer_predisable,
- };
- static struct iio_trigger *at91_adc_allocate_trigger(struct iio_dev *indio,
- char *trigger_name)
- {
- struct iio_trigger *trig;
- int ret;
- trig = devm_iio_trigger_alloc(&indio->dev, "%s-dev%d-%s", indio->name,
- indio->id, trigger_name);
- if (!trig)
- return NULL;
- trig->dev.parent = indio->dev.parent;
- iio_trigger_set_drvdata(trig, indio);
- trig->ops = &at91_adc_trigger_ops;
- ret = devm_iio_trigger_register(&indio->dev, trig);
- if (ret)
- return ERR_PTR(ret);
- return trig;
- }
- static int at91_adc_trigger_init(struct iio_dev *indio)
- {
- struct at91_adc_state *st = iio_priv(indio);
- st->trig = at91_adc_allocate_trigger(indio, st->selected_trig->name);
- if (IS_ERR(st->trig)) {
- dev_err(&indio->dev,
- "could not allocate trigger\n");
- return PTR_ERR(st->trig);
- }
- return 0;
- }
- static void at91_adc_trigger_handler_nodma(struct iio_dev *indio_dev,
- struct iio_poll_func *pf)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- int i = 0;
- int val;
- u8 bit;
- for_each_set_bit(bit, indio_dev->active_scan_mask,
- indio_dev->num_channels) {
- struct iio_chan_spec const *chan =
- at91_adc_chan_get(indio_dev, bit);
- if (!chan)
- continue;
- /*
- * Our external trigger only supports the voltage channels.
- * In case someone requested a different type of channel
- * just put zeroes to buffer.
- * This should not happen because we check the scan mode
- * and scan mask when we enable the buffer, and we don't allow
- * the buffer to start with a mixed mask (voltage and something
- * else).
- * Thus, emit a warning.
- */
- if (chan->type == IIO_VOLTAGE) {
- val = at91_adc_readl(st, chan->address);
- at91_adc_adjust_val_osr(st, &val);
- st->buffer[i] = val;
- } else {
- st->buffer[i] = 0;
- WARN(true, "This trigger cannot handle this type of channel");
- }
- i++;
- }
- iio_push_to_buffers_with_timestamp(indio_dev, st->buffer,
- pf->timestamp);
- }
- static void at91_adc_trigger_handler_dma(struct iio_dev *indio_dev)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- int transferred_len = at91_adc_dma_size_done(st);
- s64 ns = iio_get_time_ns(indio_dev);
- s64 interval;
- int sample_index = 0, sample_count, sample_size;
- u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
- /* if we reached this point, we cannot sample faster */
- if (status & AT91_SAMA5D2_IER_GOVRE)
- pr_info_ratelimited("%s: conversion overrun detected\n",
- indio_dev->name);
- sample_size = div_s64(st->dma_st.rx_buf_sz, st->dma_st.watermark);
- sample_count = div_s64(transferred_len, sample_size);
- /*
- * interval between samples is total time since last transfer handling
- * divided by the number of samples (total size divided by sample size)
- */
- interval = div_s64((ns - st->dma_st.dma_ts), sample_count);
- while (transferred_len >= sample_size) {
- /*
- * for all the values in the current sample,
- * adjust the values inside the buffer for oversampling
- */
- at91_adc_adjust_val_osr_array(st,
- &st->dma_st.rx_buf[st->dma_st.buf_idx],
- sample_size);
- iio_push_to_buffers_with_timestamp(indio_dev,
- (st->dma_st.rx_buf + st->dma_st.buf_idx),
- (st->dma_st.dma_ts + interval * sample_index));
- /* adjust remaining length */
- transferred_len -= sample_size;
- /* adjust buffer index */
- st->dma_st.buf_idx += sample_size;
- /* in case of reaching end of buffer, reset index */
- if (st->dma_st.buf_idx >= st->dma_st.rx_buf_sz)
- st->dma_st.buf_idx = 0;
- sample_index++;
- }
- /* adjust saved time for next transfer handling */
- st->dma_st.dma_ts = iio_get_time_ns(indio_dev);
- }
- static irqreturn_t at91_adc_trigger_handler(int irq, void *p)
- {
- struct iio_poll_func *pf = p;
- struct iio_dev *indio_dev = pf->indio_dev;
- struct at91_adc_state *st = iio_priv(indio_dev);
- if (st->dma_st.dma_chan)
- at91_adc_trigger_handler_dma(indio_dev);
- else
- at91_adc_trigger_handler_nodma(indio_dev, pf);
- iio_trigger_notify_done(indio_dev->trig);
- return IRQ_HANDLED;
- }
- static int at91_adc_buffer_init(struct iio_dev *indio)
- {
- struct at91_adc_state *st = iio_priv(indio);
- if (st->selected_trig->hw_trig) {
- return devm_iio_triggered_buffer_setup(&indio->dev, indio,
- &iio_pollfunc_store_time,
- &at91_adc_trigger_handler, &at91_buffer_setup_ops);
- }
- /*
- * we need to prepare the buffer ops in case we will get
- * another buffer attached (like a callback buffer for the touchscreen)
- */
- indio->setup_ops = &at91_buffer_setup_ops;
- return 0;
- }
- static unsigned at91_adc_startup_time(unsigned startup_time_min,
- unsigned adc_clk_khz)
- {
- static const unsigned int startup_lookup[] = {
- 0, 8, 16, 24,
- 64, 80, 96, 112,
- 512, 576, 640, 704,
- 768, 832, 896, 960
- };
- unsigned ticks_min, i;
- /*
- * Since the adc frequency is checked before, there is no reason
- * to not meet the startup time constraint.
- */
- ticks_min = startup_time_min * adc_clk_khz / 1000;
- for (i = 0; i < ARRAY_SIZE(startup_lookup); i++)
- if (startup_lookup[i] > ticks_min)
- break;
- return i;
- }
- static void at91_adc_setup_samp_freq(struct at91_adc_state *st, unsigned freq)
- {
- struct iio_dev *indio_dev = iio_priv_to_dev(st);
- unsigned f_per, prescal, startup, mr;
- f_per = clk_get_rate(st->per_clk);
- prescal = (f_per / (2 * freq)) - 1;
- startup = at91_adc_startup_time(st->soc_info.startup_time,
- freq / 1000);
- mr = at91_adc_readl(st, AT91_SAMA5D2_MR);
- mr &= ~(AT91_SAMA5D2_MR_STARTUP_MASK | AT91_SAMA5D2_MR_PRESCAL_MASK);
- mr |= AT91_SAMA5D2_MR_STARTUP(startup);
- mr |= AT91_SAMA5D2_MR_PRESCAL(prescal);
- at91_adc_writel(st, AT91_SAMA5D2_MR, mr);
- dev_dbg(&indio_dev->dev, "freq: %u, startup: %u, prescal: %u\n",
- freq, startup, prescal);
- st->current_sample_rate = freq;
- }
- static inline unsigned at91_adc_get_sample_freq(struct at91_adc_state *st)
- {
- return st->current_sample_rate;
- }
- static void at91_adc_touch_data_handler(struct iio_dev *indio_dev)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- u8 bit;
- u16 val;
- int i = 0;
- for_each_set_bit(bit, indio_dev->active_scan_mask,
- AT91_SAMA5D2_MAX_CHAN_IDX + 1) {
- struct iio_chan_spec const *chan =
- at91_adc_chan_get(indio_dev, bit);
- if (chan->type == IIO_POSITIONRELATIVE)
- at91_adc_read_position(st, chan->channel, &val);
- else if (chan->type == IIO_PRESSURE)
- at91_adc_read_pressure(st, chan->channel, &val);
- else
- continue;
- st->buffer[i] = val;
- i++;
- }
- /*
- * Schedule work to push to buffers.
- * This is intended to push to the callback buffer that another driver
- * registered. We are still in a handler from our IRQ. If we push
- * directly, it means the other driver has it's callback called
- * from our IRQ context. Which is something we better avoid.
- * Let's schedule it after our IRQ is completed.
- */
- schedule_work(&st->touch_st.workq);
- }
- static void at91_adc_pen_detect_interrupt(struct at91_adc_state *st)
- {
- at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_PEN);
- at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_NOPEN |
- AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
- AT91_SAMA5D2_IER_PRDY);
- at91_adc_writel(st, AT91_SAMA5D2_TRGR,
- AT91_SAMA5D2_TRGR_TRGMOD_PERIODIC |
- AT91_SAMA5D2_TRGR_TRGPER(st->touch_st.sample_period_val));
- st->touch_st.touching = true;
- }
- static void at91_adc_no_pen_detect_interrupt(struct at91_adc_state *st)
- {
- struct iio_dev *indio_dev = iio_priv_to_dev(st);
- at91_adc_writel(st, AT91_SAMA5D2_TRGR,
- AT91_SAMA5D2_TRGR_TRGMOD_NO_TRIGGER);
- at91_adc_writel(st, AT91_SAMA5D2_IDR, AT91_SAMA5D2_IER_NOPEN |
- AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
- AT91_SAMA5D2_IER_PRDY);
- st->touch_st.touching = false;
- at91_adc_touch_data_handler(indio_dev);
- at91_adc_writel(st, AT91_SAMA5D2_IER, AT91_SAMA5D2_IER_PEN);
- }
- static void at91_adc_workq_handler(struct work_struct *workq)
- {
- struct at91_adc_touch *touch_st = container_of(workq,
- struct at91_adc_touch, workq);
- struct at91_adc_state *st = container_of(touch_st,
- struct at91_adc_state, touch_st);
- struct iio_dev *indio_dev = iio_priv_to_dev(st);
- iio_push_to_buffers(indio_dev, st->buffer);
- }
- static irqreturn_t at91_adc_interrupt(int irq, void *private)
- {
- struct iio_dev *indio = private;
- struct at91_adc_state *st = iio_priv(indio);
- u32 status = at91_adc_readl(st, AT91_SAMA5D2_ISR);
- u32 imr = at91_adc_readl(st, AT91_SAMA5D2_IMR);
- u32 rdy_mask = AT91_SAMA5D2_IER_XRDY | AT91_SAMA5D2_IER_YRDY |
- AT91_SAMA5D2_IER_PRDY;
- if (!(status & imr))
- return IRQ_NONE;
- if (status & AT91_SAMA5D2_IER_PEN) {
- /* pen detected IRQ */
- at91_adc_pen_detect_interrupt(st);
- } else if ((status & AT91_SAMA5D2_IER_NOPEN)) {
- /* nopen detected IRQ */
- at91_adc_no_pen_detect_interrupt(st);
- } else if ((status & AT91_SAMA5D2_ISR_PENS) &&
- ((status & rdy_mask) == rdy_mask)) {
- /* periodic trigger IRQ - during pen sense */
- at91_adc_touch_data_handler(indio);
- } else if (status & AT91_SAMA5D2_ISR_PENS) {
- /*
- * touching, but the measurements are not ready yet.
- * read and ignore.
- */
- status = at91_adc_readl(st, AT91_SAMA5D2_XPOSR);
- status = at91_adc_readl(st, AT91_SAMA5D2_YPOSR);
- status = at91_adc_readl(st, AT91_SAMA5D2_PRESSR);
- } else if (iio_buffer_enabled(indio) && !st->dma_st.dma_chan) {
- /* triggered buffer without DMA */
- disable_irq_nosync(irq);
- iio_trigger_poll(indio->trig);
- } else if (iio_buffer_enabled(indio) && st->dma_st.dma_chan) {
- /* triggered buffer with DMA - should not happen */
- disable_irq_nosync(irq);
- WARN(true, "Unexpected irq occurred\n");
- } else if (!iio_buffer_enabled(indio)) {
- /* software requested conversion */
- st->conversion_value = at91_adc_readl(st, st->chan->address);
- st->conversion_done = true;
- wake_up_interruptible(&st->wq_data_available);
- }
- return IRQ_HANDLED;
- }
- static int at91_adc_read_info_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan, int *val)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- u32 cor = 0;
- u16 tmp_val;
- int ret;
- /*
- * Keep in mind that we cannot use software trigger or touchscreen
- * if external trigger is enabled
- */
- if (chan->type == IIO_POSITIONRELATIVE) {
- ret = iio_device_claim_direct_mode(indio_dev);
- if (ret)
- return ret;
- mutex_lock(&st->lock);
- ret = at91_adc_read_position(st, chan->channel,
- &tmp_val);
- *val = tmp_val;
- mutex_unlock(&st->lock);
- iio_device_release_direct_mode(indio_dev);
- return at91_adc_adjust_val_osr(st, val);
- }
- if (chan->type == IIO_PRESSURE) {
- ret = iio_device_claim_direct_mode(indio_dev);
- if (ret)
- return ret;
- mutex_lock(&st->lock);
- ret = at91_adc_read_pressure(st, chan->channel,
- &tmp_val);
- *val = tmp_val;
- mutex_unlock(&st->lock);
- iio_device_release_direct_mode(indio_dev);
- return at91_adc_adjust_val_osr(st, val);
- }
- /* in this case we have a voltage channel */
- ret = iio_device_claim_direct_mode(indio_dev);
- if (ret)
- return ret;
- mutex_lock(&st->lock);
- st->chan = chan;
- if (chan->differential)
- cor = (BIT(chan->channel) | BIT(chan->channel2)) <<
- AT91_SAMA5D2_COR_DIFF_OFFSET;
- at91_adc_writel(st, AT91_SAMA5D2_COR, cor);
- at91_adc_writel(st, AT91_SAMA5D2_CHER, BIT(chan->channel));
- at91_adc_writel(st, AT91_SAMA5D2_IER, BIT(chan->channel));
- at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_START);
- ret = wait_event_interruptible_timeout(st->wq_data_available,
- st->conversion_done,
- msecs_to_jiffies(1000));
- if (ret == 0)
- ret = -ETIMEDOUT;
- if (ret > 0) {
- *val = st->conversion_value;
- ret = at91_adc_adjust_val_osr(st, val);
- if (chan->scan_type.sign == 's')
- *val = sign_extend32(*val, 11);
- st->conversion_done = false;
- }
- at91_adc_writel(st, AT91_SAMA5D2_IDR, BIT(chan->channel));
- at91_adc_writel(st, AT91_SAMA5D2_CHDR, BIT(chan->channel));
- /* Needed to ACK the DRDY interruption */
- at91_adc_readl(st, AT91_SAMA5D2_LCDR);
- mutex_unlock(&st->lock);
- iio_device_release_direct_mode(indio_dev);
- return ret;
- }
- static int at91_adc_read_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int *val, int *val2, long mask)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- switch (mask) {
- case IIO_CHAN_INFO_RAW:
- return at91_adc_read_info_raw(indio_dev, chan, val);
- case IIO_CHAN_INFO_SCALE:
- *val = st->vref_uv / 1000;
- if (chan->differential)
- *val *= 2;
- *val2 = chan->scan_type.realbits;
- return IIO_VAL_FRACTIONAL_LOG2;
- case IIO_CHAN_INFO_SAMP_FREQ:
- *val = at91_adc_get_sample_freq(st);
- return IIO_VAL_INT;
- case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
- *val = st->oversampling_ratio;
- return IIO_VAL_INT;
- default:
- return -EINVAL;
- }
- }
- static int at91_adc_write_raw(struct iio_dev *indio_dev,
- struct iio_chan_spec const *chan,
- int val, int val2, long mask)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- switch (mask) {
- case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
- if ((val != AT91_OSR_1SAMPLES) && (val != AT91_OSR_4SAMPLES) &&
- (val != AT91_OSR_16SAMPLES))
- return -EINVAL;
- /* if no change, optimize out */
- if (val == st->oversampling_ratio)
- return 0;
- st->oversampling_ratio = val;
- /* update ratio */
- at91_adc_config_emr(st);
- return 0;
- case IIO_CHAN_INFO_SAMP_FREQ:
- if (val < st->soc_info.min_sample_rate ||
- val > st->soc_info.max_sample_rate)
- return -EINVAL;
- at91_adc_setup_samp_freq(st, val);
- return 0;
- default:
- return -EINVAL;
- };
- }
- static void at91_adc_dma_init(struct platform_device *pdev)
- {
- struct iio_dev *indio_dev = platform_get_drvdata(pdev);
- struct at91_adc_state *st = iio_priv(indio_dev);
- struct dma_slave_config config = {0};
- /*
- * We make the buffer double the size of the fifo,
- * such that DMA uses one half of the buffer (full fifo size)
- * and the software uses the other half to read/write.
- */
- unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
- AT91_BUFFER_MAX_CONVERSION_BYTES * 2,
- PAGE_SIZE);
- if (st->dma_st.dma_chan)
- return;
- st->dma_st.dma_chan = dma_request_slave_channel(&pdev->dev, "rx");
- if (!st->dma_st.dma_chan) {
- dev_info(&pdev->dev, "can't get DMA channel\n");
- goto dma_exit;
- }
- st->dma_st.rx_buf = dma_alloc_coherent(st->dma_st.dma_chan->device->dev,
- pages * PAGE_SIZE,
- &st->dma_st.rx_dma_buf,
- GFP_KERNEL);
- if (!st->dma_st.rx_buf) {
- dev_info(&pdev->dev, "can't allocate coherent DMA area\n");
- goto dma_chan_disable;
- }
- /* Configure DMA channel to read data register */
- config.direction = DMA_DEV_TO_MEM;
- config.src_addr = (phys_addr_t)(st->dma_st.phys_addr
- + AT91_SAMA5D2_LCDR);
- config.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
- config.src_maxburst = 1;
- config.dst_maxburst = 1;
- if (dmaengine_slave_config(st->dma_st.dma_chan, &config)) {
- dev_info(&pdev->dev, "can't configure DMA slave\n");
- goto dma_free_area;
- }
- dev_info(&pdev->dev, "using %s for rx DMA transfers\n",
- dma_chan_name(st->dma_st.dma_chan));
- return;
- dma_free_area:
- dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE,
- st->dma_st.rx_buf, st->dma_st.rx_dma_buf);
- dma_chan_disable:
- dma_release_channel(st->dma_st.dma_chan);
- st->dma_st.dma_chan = 0;
- dma_exit:
- dev_info(&pdev->dev, "continuing without DMA support\n");
- }
- static void at91_adc_dma_disable(struct platform_device *pdev)
- {
- struct iio_dev *indio_dev = platform_get_drvdata(pdev);
- struct at91_adc_state *st = iio_priv(indio_dev);
- unsigned int pages = DIV_ROUND_UP(AT91_HWFIFO_MAX_SIZE *
- AT91_BUFFER_MAX_CONVERSION_BYTES * 2,
- PAGE_SIZE);
- /* if we are not using DMA, just return */
- if (!st->dma_st.dma_chan)
- return;
- /* wait for all transactions to be terminated first*/
- dmaengine_terminate_sync(st->dma_st.dma_chan);
- dma_free_coherent(st->dma_st.dma_chan->device->dev, pages * PAGE_SIZE,
- st->dma_st.rx_buf, st->dma_st.rx_dma_buf);
- dma_release_channel(st->dma_st.dma_chan);
- st->dma_st.dma_chan = 0;
- dev_info(&pdev->dev, "continuing without DMA support\n");
- }
- static int at91_adc_set_watermark(struct iio_dev *indio_dev, unsigned int val)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- if (val > AT91_HWFIFO_MAX_SIZE)
- return -EINVAL;
- if (!st->selected_trig->hw_trig) {
- dev_dbg(&indio_dev->dev, "we need hw trigger for DMA\n");
- return 0;
- }
- dev_dbg(&indio_dev->dev, "new watermark is %u\n", val);
- st->dma_st.watermark = val;
- /*
- * The logic here is: if we have watermark 1, it means we do
- * each conversion with it's own IRQ, thus we don't need DMA.
- * If the watermark is higher, we do DMA to do all the transfers in bulk
- */
- if (val == 1)
- at91_adc_dma_disable(to_platform_device(&indio_dev->dev));
- else if (val > 1)
- at91_adc_dma_init(to_platform_device(&indio_dev->dev));
- return 0;
- }
- static int at91_adc_update_scan_mode(struct iio_dev *indio_dev,
- const unsigned long *scan_mask)
- {
- struct at91_adc_state *st = iio_priv(indio_dev);
- if (bitmap_subset(scan_mask, &st->touch_st.channels_bitmask,
- AT91_SAMA5D2_MAX_CHAN_IDX + 1))
- return 0;
- /*
- * if the new bitmap is a combination of touchscreen and regular
- * channels, then we are not fine
- */
- if (bitmap_intersects(&st->touch_st.channels_bitmask, scan_mask,
- AT91_SAMA5D2_MAX_CHAN_IDX + 1))
- return -EINVAL;
- return 0;
- }
- static void at91_adc_hw_init(struct at91_adc_state *st)
- {
- at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
- at91_adc_writel(st, AT91_SAMA5D2_IDR, 0xffffffff);
- /*
- * Transfer field must be set to 2 according to the datasheet and
- * allows different analog settings for each channel.
- */
- at91_adc_writel(st, AT91_SAMA5D2_MR,
- AT91_SAMA5D2_MR_TRANSFER(2) | AT91_SAMA5D2_MR_ANACH);
- at91_adc_setup_samp_freq(st, st->soc_info.min_sample_rate);
- /* configure extended mode register */
- at91_adc_config_emr(st);
- }
- static ssize_t at91_adc_get_fifo_state(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct iio_dev *indio_dev =
- platform_get_drvdata(to_platform_device(dev));
- struct at91_adc_state *st = iio_priv(indio_dev);
- return scnprintf(buf, PAGE_SIZE, "%d\n", !!st->dma_st.dma_chan);
- }
- static ssize_t at91_adc_get_watermark(struct device *dev,
- struct device_attribute *attr, char *buf)
- {
- struct iio_dev *indio_dev =
- platform_get_drvdata(to_platform_device(dev));
- struct at91_adc_state *st = iio_priv(indio_dev);
- return scnprintf(buf, PAGE_SIZE, "%d\n", st->dma_st.watermark);
- }
- static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
- at91_adc_get_fifo_state, NULL, 0);
- static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
- at91_adc_get_watermark, NULL, 0);
- static IIO_CONST_ATTR(hwfifo_watermark_min, "2");
- static IIO_CONST_ATTR(hwfifo_watermark_max, AT91_HWFIFO_MAX_SIZE_STR);
- static IIO_CONST_ATTR(oversampling_ratio_available,
- __stringify(AT91_OSR_1SAMPLES) " "
- __stringify(AT91_OSR_4SAMPLES) " "
- __stringify(AT91_OSR_16SAMPLES));
- static struct attribute *at91_adc_attributes[] = {
- &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
- NULL,
- };
- static const struct attribute_group at91_adc_attribute_group = {
- .attrs = at91_adc_attributes,
- };
- static const struct attribute *at91_adc_fifo_attributes[] = {
- &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
- &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
- &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
- &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
- NULL,
- };
- static const struct iio_info at91_adc_info = {
- .attrs = &at91_adc_attribute_group,
- .read_raw = &at91_adc_read_raw,
- .write_raw = &at91_adc_write_raw,
- .update_scan_mode = &at91_adc_update_scan_mode,
- .of_xlate = &at91_adc_of_xlate,
- .hwfifo_set_watermark = &at91_adc_set_watermark,
- };
- static int at91_adc_probe(struct platform_device *pdev)
- {
- struct iio_dev *indio_dev;
- struct at91_adc_state *st;
- struct resource *res;
- int ret, i;
- u32 edge_type = IRQ_TYPE_NONE;
- indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st));
- if (!indio_dev)
- return -ENOMEM;
- indio_dev->dev.parent = &pdev->dev;
- indio_dev->name = dev_name(&pdev->dev);
- indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
- indio_dev->info = &at91_adc_info;
- indio_dev->channels = at91_adc_channels;
- indio_dev->num_channels = ARRAY_SIZE(at91_adc_channels);
- st = iio_priv(indio_dev);
- bitmap_set(&st->touch_st.channels_bitmask,
- AT91_SAMA5D2_TOUCH_X_CHAN_IDX, 1);
- bitmap_set(&st->touch_st.channels_bitmask,
- AT91_SAMA5D2_TOUCH_Y_CHAN_IDX, 1);
- bitmap_set(&st->touch_st.channels_bitmask,
- AT91_SAMA5D2_TOUCH_P_CHAN_IDX, 1);
- st->oversampling_ratio = AT91_OSR_1SAMPLES;
- ret = of_property_read_u32(pdev->dev.of_node,
- "atmel,min-sample-rate-hz",
- &st->soc_info.min_sample_rate);
- if (ret) {
- dev_err(&pdev->dev,
- "invalid or missing value for atmel,min-sample-rate-hz\n");
- return ret;
- }
- ret = of_property_read_u32(pdev->dev.of_node,
- "atmel,max-sample-rate-hz",
- &st->soc_info.max_sample_rate);
- if (ret) {
- dev_err(&pdev->dev,
- "invalid or missing value for atmel,max-sample-rate-hz\n");
- return ret;
- }
- ret = of_property_read_u32(pdev->dev.of_node, "atmel,startup-time-ms",
- &st->soc_info.startup_time);
- if (ret) {
- dev_err(&pdev->dev,
- "invalid or missing value for atmel,startup-time-ms\n");
- return ret;
- }
- ret = of_property_read_u32(pdev->dev.of_node,
- "atmel,trigger-edge-type", &edge_type);
- if (ret) {
- dev_dbg(&pdev->dev,
- "atmel,trigger-edge-type not specified, only software trigger available\n");
- }
- st->selected_trig = NULL;
- /* find the right trigger, or no trigger at all */
- for (i = 0; i < AT91_SAMA5D2_HW_TRIG_CNT + 1; i++)
- if (at91_adc_trigger_list[i].edge_type == edge_type) {
- st->selected_trig = &at91_adc_trigger_list[i];
- break;
- }
- if (!st->selected_trig) {
- dev_err(&pdev->dev, "invalid external trigger edge value\n");
- return -EINVAL;
- }
- init_waitqueue_head(&st->wq_data_available);
- mutex_init(&st->lock);
- INIT_WORK(&st->touch_st.workq, at91_adc_workq_handler);
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!res)
- return -EINVAL;
- /* if we plan to use DMA, we need the physical address of the regs */
- st->dma_st.phys_addr = res->start;
- st->base = devm_ioremap_resource(&pdev->dev, res);
- if (IS_ERR(st->base))
- return PTR_ERR(st->base);
- st->irq = platform_get_irq(pdev, 0);
- if (st->irq <= 0) {
- if (!st->irq)
- st->irq = -ENXIO;
- return st->irq;
- }
- st->per_clk = devm_clk_get(&pdev->dev, "adc_clk");
- if (IS_ERR(st->per_clk))
- return PTR_ERR(st->per_clk);
- st->reg = devm_regulator_get(&pdev->dev, "vddana");
- if (IS_ERR(st->reg))
- return PTR_ERR(st->reg);
- st->vref = devm_regulator_get(&pdev->dev, "vref");
- if (IS_ERR(st->vref))
- return PTR_ERR(st->vref);
- ret = devm_request_irq(&pdev->dev, st->irq, at91_adc_interrupt, 0,
- pdev->dev.driver->name, indio_dev);
- if (ret)
- return ret;
- ret = regulator_enable(st->reg);
- if (ret)
- return ret;
- ret = regulator_enable(st->vref);
- if (ret)
- goto reg_disable;
- st->vref_uv = regulator_get_voltage(st->vref);
- if (st->vref_uv <= 0) {
- ret = -EINVAL;
- goto vref_disable;
- }
- at91_adc_hw_init(st);
- ret = clk_prepare_enable(st->per_clk);
- if (ret)
- goto vref_disable;
- platform_set_drvdata(pdev, indio_dev);
- ret = at91_adc_buffer_init(indio_dev);
- if (ret < 0) {
- dev_err(&pdev->dev, "couldn't initialize the buffer.\n");
- goto per_clk_disable_unprepare;
- }
- if (st->selected_trig->hw_trig) {
- ret = at91_adc_trigger_init(indio_dev);
- if (ret < 0) {
- dev_err(&pdev->dev, "couldn't setup the triggers.\n");
- goto per_clk_disable_unprepare;
- }
- /*
- * Initially the iio buffer has a length of 2 and
- * a watermark of 1
- */
- st->dma_st.watermark = 1;
- iio_buffer_set_attrs(indio_dev->buffer,
- at91_adc_fifo_attributes);
- }
- if (dma_coerce_mask_and_coherent(&indio_dev->dev, DMA_BIT_MASK(32)))
- dev_info(&pdev->dev, "cannot set DMA mask to 32-bit\n");
- ret = iio_device_register(indio_dev);
- if (ret < 0)
- goto dma_disable;
- if (st->selected_trig->hw_trig)
- dev_info(&pdev->dev, "setting up trigger as %s\n",
- st->selected_trig->name);
- dev_info(&pdev->dev, "version: %x\n",
- readl_relaxed(st->base + AT91_SAMA5D2_VERSION));
- return 0;
- dma_disable:
- at91_adc_dma_disable(pdev);
- per_clk_disable_unprepare:
- clk_disable_unprepare(st->per_clk);
- vref_disable:
- regulator_disable(st->vref);
- reg_disable:
- regulator_disable(st->reg);
- return ret;
- }
- static int at91_adc_remove(struct platform_device *pdev)
- {
- struct iio_dev *indio_dev = platform_get_drvdata(pdev);
- struct at91_adc_state *st = iio_priv(indio_dev);
- iio_device_unregister(indio_dev);
- at91_adc_dma_disable(pdev);
- clk_disable_unprepare(st->per_clk);
- regulator_disable(st->vref);
- regulator_disable(st->reg);
- return 0;
- }
- static __maybe_unused int at91_adc_suspend(struct device *dev)
- {
- struct iio_dev *indio_dev =
- platform_get_drvdata(to_platform_device(dev));
- struct at91_adc_state *st = iio_priv(indio_dev);
- /*
- * Do a sofware reset of the ADC before we go to suspend.
- * this will ensure that all pins are free from being muxed by the ADC
- * and can be used by for other devices.
- * Otherwise, ADC will hog them and we can't go to suspend mode.
- */
- at91_adc_writel(st, AT91_SAMA5D2_CR, AT91_SAMA5D2_CR_SWRST);
- clk_disable_unprepare(st->per_clk);
- regulator_disable(st->vref);
- regulator_disable(st->reg);
- return pinctrl_pm_select_sleep_state(dev);
- }
- static __maybe_unused int at91_adc_resume(struct device *dev)
- {
- struct iio_dev *indio_dev =
- platform_get_drvdata(to_platform_device(dev));
- struct at91_adc_state *st = iio_priv(indio_dev);
- int ret;
- ret = pinctrl_pm_select_default_state(dev);
- if (ret)
- goto resume_failed;
- ret = regulator_enable(st->reg);
- if (ret)
- goto resume_failed;
- ret = regulator_enable(st->vref);
- if (ret)
- goto reg_disable_resume;
- ret = clk_prepare_enable(st->per_clk);
- if (ret)
- goto vref_disable_resume;
- at91_adc_hw_init(st);
- /* reconfiguring trigger hardware state */
- if (!iio_buffer_enabled(indio_dev))
- return 0;
- /* check if we are enabling triggered buffer or the touchscreen */
- if (bitmap_subset(indio_dev->active_scan_mask,
- &st->touch_st.channels_bitmask,
- AT91_SAMA5D2_MAX_CHAN_IDX + 1)) {
- /* touchscreen enabling */
- return at91_adc_configure_touch(st, true);
- } else {
- return at91_adc_configure_trigger(st->trig, true);
- }
- /* not needed but more explicit */
- return 0;
- vref_disable_resume:
- regulator_disable(st->vref);
- reg_disable_resume:
- regulator_disable(st->reg);
- resume_failed:
- dev_err(&indio_dev->dev, "failed to resume\n");
- return ret;
- }
- static SIMPLE_DEV_PM_OPS(at91_adc_pm_ops, at91_adc_suspend, at91_adc_resume);
- static const struct of_device_id at91_adc_dt_match[] = {
- {
- .compatible = "atmel,sama5d2-adc",
- }, {
- /* sentinel */
- }
- };
- MODULE_DEVICE_TABLE(of, at91_adc_dt_match);
- static struct platform_driver at91_adc_driver = {
- .probe = at91_adc_probe,
- .remove = at91_adc_remove,
- .driver = {
- .name = "at91-sama5d2_adc",
- .of_match_table = at91_adc_dt_match,
- .pm = &at91_adc_pm_ops,
- },
- };
- module_platform_driver(at91_adc_driver)
- MODULE_AUTHOR("Ludovic Desroches <ludovic.desroches@atmel.com>");
- MODULE_DESCRIPTION("Atmel AT91 SAMA5D2 ADC");
- MODULE_LICENSE("GPL v2");
|