exynos_adc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * exynos_adc.c - Support for ADC in EXYNOS SoCs
  4. *
  5. * 8 ~ 10 channel, 10/12-bit ADC
  6. *
  7. * Copyright (C) 2013 Naveen Krishna Chatradhi <[email protected]>
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/io.h>
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/err.h>
  25. #include <linux/input.h>
  26. #include <linux/iio/iio.h>
  27. #include <linux/iio/machine.h>
  28. #include <linux/iio/driver.h>
  29. #include <linux/mfd/syscon.h>
  30. #include <linux/regmap.h>
  31. #include <linux/platform_data/touchscreen-s3c2410.h>
  32. /* S3C/EXYNOS4412/5250 ADC_V1 registers definitions */
  33. #define ADC_V1_CON(x) ((x) + 0x00)
  34. #define ADC_V1_TSC(x) ((x) + 0x04)
  35. #define ADC_V1_DLY(x) ((x) + 0x08)
  36. #define ADC_V1_DATX(x) ((x) + 0x0C)
  37. #define ADC_V1_DATY(x) ((x) + 0x10)
  38. #define ADC_V1_UPDN(x) ((x) + 0x14)
  39. #define ADC_V1_INTCLR(x) ((x) + 0x18)
  40. #define ADC_V1_MUX(x) ((x) + 0x1c)
  41. #define ADC_V1_CLRINTPNDNUP(x) ((x) + 0x20)
  42. /* S3C2410 ADC registers definitions */
  43. #define ADC_S3C2410_MUX(x) ((x) + 0x18)
  44. /* Future ADC_V2 registers definitions */
  45. #define ADC_V2_CON1(x) ((x) + 0x00)
  46. #define ADC_V2_CON2(x) ((x) + 0x04)
  47. #define ADC_V2_STAT(x) ((x) + 0x08)
  48. #define ADC_V2_INT_EN(x) ((x) + 0x10)
  49. #define ADC_V2_INT_ST(x) ((x) + 0x14)
  50. #define ADC_V2_VER(x) ((x) + 0x20)
  51. /* Bit definitions for ADC_V1 */
  52. #define ADC_V1_CON_RES (1u << 16)
  53. #define ADC_V1_CON_PRSCEN (1u << 14)
  54. #define ADC_V1_CON_PRSCLV(x) (((x) & 0xFF) << 6)
  55. #define ADC_V1_CON_STANDBY (1u << 2)
  56. /* Bit definitions for S3C2410 ADC */
  57. #define ADC_S3C2410_CON_SELMUX(x) (((x) & 7) << 3)
  58. #define ADC_S3C2410_DATX_MASK 0x3FF
  59. #define ADC_S3C2416_CON_RES_SEL (1u << 3)
  60. /* touch screen always uses channel 0 */
  61. #define ADC_S3C2410_MUX_TS 0
  62. /* ADCTSC Register Bits */
  63. #define ADC_S3C2443_TSC_UD_SEN (1u << 8)
  64. #define ADC_S3C2410_TSC_YM_SEN (1u << 7)
  65. #define ADC_S3C2410_TSC_YP_SEN (1u << 6)
  66. #define ADC_S3C2410_TSC_XM_SEN (1u << 5)
  67. #define ADC_S3C2410_TSC_XP_SEN (1u << 4)
  68. #define ADC_S3C2410_TSC_PULL_UP_DISABLE (1u << 3)
  69. #define ADC_S3C2410_TSC_AUTO_PST (1u << 2)
  70. #define ADC_S3C2410_TSC_XY_PST(x) (((x) & 0x3) << 0)
  71. #define ADC_TSC_WAIT4INT (ADC_S3C2410_TSC_YM_SEN | \
  72. ADC_S3C2410_TSC_YP_SEN | \
  73. ADC_S3C2410_TSC_XP_SEN | \
  74. ADC_S3C2410_TSC_XY_PST(3))
  75. #define ADC_TSC_AUTOPST (ADC_S3C2410_TSC_YM_SEN | \
  76. ADC_S3C2410_TSC_YP_SEN | \
  77. ADC_S3C2410_TSC_XP_SEN | \
  78. ADC_S3C2410_TSC_AUTO_PST | \
  79. ADC_S3C2410_TSC_XY_PST(0))
  80. /* Bit definitions for ADC_V2 */
  81. #define ADC_V2_CON1_SOFT_RESET (1u << 2)
  82. #define ADC_V2_CON2_OSEL (1u << 10)
  83. #define ADC_V2_CON2_ESEL (1u << 9)
  84. #define ADC_V2_CON2_HIGHF (1u << 8)
  85. #define ADC_V2_CON2_C_TIME(x) (((x) & 7) << 4)
  86. #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0)
  87. #define ADC_V2_CON2_ACH_MASK 0xF
  88. #define MAX_ADC_V2_CHANNELS 10
  89. #define MAX_ADC_V1_CHANNELS 8
  90. #define MAX_EXYNOS3250_ADC_CHANNELS 2
  91. #define MAX_EXYNOS4212_ADC_CHANNELS 4
  92. #define MAX_S5PV210_ADC_CHANNELS 10
  93. /* Bit definitions common for ADC_V1 and ADC_V2 */
  94. #define ADC_CON_EN_START (1u << 0)
  95. #define ADC_CON_EN_START_MASK (0x3 << 0)
  96. #define ADC_DATX_PRESSED (1u << 15)
  97. #define ADC_DATX_MASK 0xFFF
  98. #define ADC_DATY_MASK 0xFFF
  99. #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100))
  100. #define EXYNOS_ADCV1_PHY_OFFSET 0x0718
  101. #define EXYNOS_ADCV2_PHY_OFFSET 0x0720
  102. struct exynos_adc {
  103. struct exynos_adc_data *data;
  104. struct device *dev;
  105. struct input_dev *input;
  106. void __iomem *regs;
  107. struct regmap *pmu_map;
  108. struct clk *clk;
  109. struct clk *sclk;
  110. unsigned int irq;
  111. unsigned int tsirq;
  112. unsigned int delay;
  113. struct regulator *vdd;
  114. struct completion completion;
  115. u32 value;
  116. unsigned int version;
  117. bool ts_enabled;
  118. bool read_ts;
  119. u32 ts_x;
  120. u32 ts_y;
  121. /*
  122. * Lock to protect from potential concurrent access to the
  123. * completion callback during a manual conversion. For this driver
  124. * a wait-callback is used to wait for the conversion result,
  125. * so in the meantime no other read request (or conversion start)
  126. * must be performed, otherwise it would interfere with the
  127. * current conversion result.
  128. */
  129. struct mutex lock;
  130. };
  131. struct exynos_adc_data {
  132. int num_channels;
  133. bool needs_sclk;
  134. bool needs_adc_phy;
  135. int phy_offset;
  136. u32 mask;
  137. void (*init_hw)(struct exynos_adc *info);
  138. void (*exit_hw)(struct exynos_adc *info);
  139. void (*clear_irq)(struct exynos_adc *info);
  140. void (*start_conv)(struct exynos_adc *info, unsigned long addr);
  141. };
  142. static void exynos_adc_unprepare_clk(struct exynos_adc *info)
  143. {
  144. if (info->data->needs_sclk)
  145. clk_unprepare(info->sclk);
  146. clk_unprepare(info->clk);
  147. }
  148. static int exynos_adc_prepare_clk(struct exynos_adc *info)
  149. {
  150. int ret;
  151. ret = clk_prepare(info->clk);
  152. if (ret) {
  153. dev_err(info->dev, "failed preparing adc clock: %d\n", ret);
  154. return ret;
  155. }
  156. if (info->data->needs_sclk) {
  157. ret = clk_prepare(info->sclk);
  158. if (ret) {
  159. clk_unprepare(info->clk);
  160. dev_err(info->dev,
  161. "failed preparing sclk_adc clock: %d\n", ret);
  162. return ret;
  163. }
  164. }
  165. return 0;
  166. }
  167. static void exynos_adc_disable_clk(struct exynos_adc *info)
  168. {
  169. if (info->data->needs_sclk)
  170. clk_disable(info->sclk);
  171. clk_disable(info->clk);
  172. }
  173. static int exynos_adc_enable_clk(struct exynos_adc *info)
  174. {
  175. int ret;
  176. ret = clk_enable(info->clk);
  177. if (ret) {
  178. dev_err(info->dev, "failed enabling adc clock: %d\n", ret);
  179. return ret;
  180. }
  181. if (info->data->needs_sclk) {
  182. ret = clk_enable(info->sclk);
  183. if (ret) {
  184. clk_disable(info->clk);
  185. dev_err(info->dev,
  186. "failed enabling sclk_adc clock: %d\n", ret);
  187. return ret;
  188. }
  189. }
  190. return 0;
  191. }
  192. static void exynos_adc_v1_init_hw(struct exynos_adc *info)
  193. {
  194. u32 con1;
  195. if (info->data->needs_adc_phy)
  196. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  197. /* set default prescaler values and Enable prescaler */
  198. con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN;
  199. /* Enable 12-bit ADC resolution */
  200. con1 |= ADC_V1_CON_RES;
  201. writel(con1, ADC_V1_CON(info->regs));
  202. /* set touchscreen delay */
  203. writel(info->delay, ADC_V1_DLY(info->regs));
  204. }
  205. static void exynos_adc_v1_exit_hw(struct exynos_adc *info)
  206. {
  207. u32 con;
  208. if (info->data->needs_adc_phy)
  209. regmap_write(info->pmu_map, info->data->phy_offset, 0);
  210. con = readl(ADC_V1_CON(info->regs));
  211. con |= ADC_V1_CON_STANDBY;
  212. writel(con, ADC_V1_CON(info->regs));
  213. }
  214. static void exynos_adc_v1_clear_irq(struct exynos_adc *info)
  215. {
  216. writel(1, ADC_V1_INTCLR(info->regs));
  217. }
  218. static void exynos_adc_v1_start_conv(struct exynos_adc *info,
  219. unsigned long addr)
  220. {
  221. u32 con1;
  222. writel(addr, ADC_V1_MUX(info->regs));
  223. con1 = readl(ADC_V1_CON(info->regs));
  224. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  225. }
  226. /* Exynos4212 and 4412 is like ADCv1 but with four channels only */
  227. static const struct exynos_adc_data exynos4212_adc_data = {
  228. .num_channels = MAX_EXYNOS4212_ADC_CHANNELS,
  229. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  230. .needs_adc_phy = true,
  231. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  232. .init_hw = exynos_adc_v1_init_hw,
  233. .exit_hw = exynos_adc_v1_exit_hw,
  234. .clear_irq = exynos_adc_v1_clear_irq,
  235. .start_conv = exynos_adc_v1_start_conv,
  236. };
  237. static const struct exynos_adc_data exynos_adc_v1_data = {
  238. .num_channels = MAX_ADC_V1_CHANNELS,
  239. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  240. .needs_adc_phy = true,
  241. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  242. .init_hw = exynos_adc_v1_init_hw,
  243. .exit_hw = exynos_adc_v1_exit_hw,
  244. .clear_irq = exynos_adc_v1_clear_irq,
  245. .start_conv = exynos_adc_v1_start_conv,
  246. };
  247. static const struct exynos_adc_data exynos_adc_s5pv210_data = {
  248. .num_channels = MAX_S5PV210_ADC_CHANNELS,
  249. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  250. .init_hw = exynos_adc_v1_init_hw,
  251. .exit_hw = exynos_adc_v1_exit_hw,
  252. .clear_irq = exynos_adc_v1_clear_irq,
  253. .start_conv = exynos_adc_v1_start_conv,
  254. };
  255. static void exynos_adc_s3c2416_start_conv(struct exynos_adc *info,
  256. unsigned long addr)
  257. {
  258. u32 con1;
  259. /* Enable 12 bit ADC resolution */
  260. con1 = readl(ADC_V1_CON(info->regs));
  261. con1 |= ADC_S3C2416_CON_RES_SEL;
  262. writel(con1, ADC_V1_CON(info->regs));
  263. /* Select channel for S3C2416 */
  264. writel(addr, ADC_S3C2410_MUX(info->regs));
  265. con1 = readl(ADC_V1_CON(info->regs));
  266. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  267. }
  268. static struct exynos_adc_data const exynos_adc_s3c2416_data = {
  269. .num_channels = MAX_ADC_V1_CHANNELS,
  270. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  271. .init_hw = exynos_adc_v1_init_hw,
  272. .exit_hw = exynos_adc_v1_exit_hw,
  273. .start_conv = exynos_adc_s3c2416_start_conv,
  274. };
  275. static void exynos_adc_s3c2443_start_conv(struct exynos_adc *info,
  276. unsigned long addr)
  277. {
  278. u32 con1;
  279. /* Select channel for S3C2433 */
  280. writel(addr, ADC_S3C2410_MUX(info->regs));
  281. con1 = readl(ADC_V1_CON(info->regs));
  282. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  283. }
  284. static struct exynos_adc_data const exynos_adc_s3c2443_data = {
  285. .num_channels = MAX_ADC_V1_CHANNELS,
  286. .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
  287. .init_hw = exynos_adc_v1_init_hw,
  288. .exit_hw = exynos_adc_v1_exit_hw,
  289. .start_conv = exynos_adc_s3c2443_start_conv,
  290. };
  291. static void exynos_adc_s3c64xx_start_conv(struct exynos_adc *info,
  292. unsigned long addr)
  293. {
  294. u32 con1;
  295. con1 = readl(ADC_V1_CON(info->regs));
  296. con1 &= ~ADC_S3C2410_CON_SELMUX(0x7);
  297. con1 |= ADC_S3C2410_CON_SELMUX(addr);
  298. writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs));
  299. }
  300. static struct exynos_adc_data const exynos_adc_s3c24xx_data = {
  301. .num_channels = MAX_ADC_V1_CHANNELS,
  302. .mask = ADC_S3C2410_DATX_MASK, /* 10 bit ADC resolution */
  303. .init_hw = exynos_adc_v1_init_hw,
  304. .exit_hw = exynos_adc_v1_exit_hw,
  305. .start_conv = exynos_adc_s3c64xx_start_conv,
  306. };
  307. static struct exynos_adc_data const exynos_adc_s3c64xx_data = {
  308. .num_channels = MAX_ADC_V1_CHANNELS,
  309. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  310. .init_hw = exynos_adc_v1_init_hw,
  311. .exit_hw = exynos_adc_v1_exit_hw,
  312. .clear_irq = exynos_adc_v1_clear_irq,
  313. .start_conv = exynos_adc_s3c64xx_start_conv,
  314. };
  315. static void exynos_adc_v2_init_hw(struct exynos_adc *info)
  316. {
  317. u32 con1, con2;
  318. if (info->data->needs_adc_phy)
  319. regmap_write(info->pmu_map, info->data->phy_offset, 1);
  320. con1 = ADC_V2_CON1_SOFT_RESET;
  321. writel(con1, ADC_V2_CON1(info->regs));
  322. con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL |
  323. ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0);
  324. writel(con2, ADC_V2_CON2(info->regs));
  325. /* Enable interrupts */
  326. writel(1, ADC_V2_INT_EN(info->regs));
  327. }
  328. static void exynos_adc_v2_exit_hw(struct exynos_adc *info)
  329. {
  330. u32 con;
  331. if (info->data->needs_adc_phy)
  332. regmap_write(info->pmu_map, info->data->phy_offset, 0);
  333. con = readl(ADC_V2_CON1(info->regs));
  334. con &= ~ADC_CON_EN_START;
  335. writel(con, ADC_V2_CON1(info->regs));
  336. }
  337. static void exynos_adc_v2_clear_irq(struct exynos_adc *info)
  338. {
  339. writel(1, ADC_V2_INT_ST(info->regs));
  340. }
  341. static void exynos_adc_v2_start_conv(struct exynos_adc *info,
  342. unsigned long addr)
  343. {
  344. u32 con1, con2;
  345. con2 = readl(ADC_V2_CON2(info->regs));
  346. con2 &= ~ADC_V2_CON2_ACH_MASK;
  347. con2 |= ADC_V2_CON2_ACH_SEL(addr);
  348. writel(con2, ADC_V2_CON2(info->regs));
  349. con1 = readl(ADC_V2_CON1(info->regs));
  350. writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs));
  351. }
  352. static const struct exynos_adc_data exynos_adc_v2_data = {
  353. .num_channels = MAX_ADC_V2_CHANNELS,
  354. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  355. .needs_adc_phy = true,
  356. .phy_offset = EXYNOS_ADCV2_PHY_OFFSET,
  357. .init_hw = exynos_adc_v2_init_hw,
  358. .exit_hw = exynos_adc_v2_exit_hw,
  359. .clear_irq = exynos_adc_v2_clear_irq,
  360. .start_conv = exynos_adc_v2_start_conv,
  361. };
  362. static const struct exynos_adc_data exynos3250_adc_data = {
  363. .num_channels = MAX_EXYNOS3250_ADC_CHANNELS,
  364. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  365. .needs_sclk = true,
  366. .needs_adc_phy = true,
  367. .phy_offset = EXYNOS_ADCV1_PHY_OFFSET,
  368. .init_hw = exynos_adc_v2_init_hw,
  369. .exit_hw = exynos_adc_v2_exit_hw,
  370. .clear_irq = exynos_adc_v2_clear_irq,
  371. .start_conv = exynos_adc_v2_start_conv,
  372. };
  373. static void exynos_adc_exynos7_init_hw(struct exynos_adc *info)
  374. {
  375. u32 con1, con2;
  376. con1 = ADC_V2_CON1_SOFT_RESET;
  377. writel(con1, ADC_V2_CON1(info->regs));
  378. con2 = readl(ADC_V2_CON2(info->regs));
  379. con2 &= ~ADC_V2_CON2_C_TIME(7);
  380. con2 |= ADC_V2_CON2_C_TIME(0);
  381. writel(con2, ADC_V2_CON2(info->regs));
  382. /* Enable interrupts */
  383. writel(1, ADC_V2_INT_EN(info->regs));
  384. }
  385. static const struct exynos_adc_data exynos7_adc_data = {
  386. .num_channels = MAX_ADC_V1_CHANNELS,
  387. .mask = ADC_DATX_MASK, /* 12 bit ADC resolution */
  388. .init_hw = exynos_adc_exynos7_init_hw,
  389. .exit_hw = exynos_adc_v2_exit_hw,
  390. .clear_irq = exynos_adc_v2_clear_irq,
  391. .start_conv = exynos_adc_v2_start_conv,
  392. };
  393. static const struct of_device_id exynos_adc_match[] = {
  394. {
  395. .compatible = "samsung,s3c2410-adc",
  396. .data = &exynos_adc_s3c24xx_data,
  397. }, {
  398. .compatible = "samsung,s3c2416-adc",
  399. .data = &exynos_adc_s3c2416_data,
  400. }, {
  401. .compatible = "samsung,s3c2440-adc",
  402. .data = &exynos_adc_s3c24xx_data,
  403. }, {
  404. .compatible = "samsung,s3c2443-adc",
  405. .data = &exynos_adc_s3c2443_data,
  406. }, {
  407. .compatible = "samsung,s3c6410-adc",
  408. .data = &exynos_adc_s3c64xx_data,
  409. }, {
  410. .compatible = "samsung,s5pv210-adc",
  411. .data = &exynos_adc_s5pv210_data,
  412. }, {
  413. .compatible = "samsung,exynos4212-adc",
  414. .data = &exynos4212_adc_data,
  415. }, {
  416. .compatible = "samsung,exynos-adc-v1",
  417. .data = &exynos_adc_v1_data,
  418. }, {
  419. .compatible = "samsung,exynos-adc-v2",
  420. .data = &exynos_adc_v2_data,
  421. }, {
  422. .compatible = "samsung,exynos3250-adc",
  423. .data = &exynos3250_adc_data,
  424. }, {
  425. .compatible = "samsung,exynos7-adc",
  426. .data = &exynos7_adc_data,
  427. },
  428. {},
  429. };
  430. MODULE_DEVICE_TABLE(of, exynos_adc_match);
  431. static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev)
  432. {
  433. const struct of_device_id *match;
  434. match = of_match_node(exynos_adc_match, pdev->dev.of_node);
  435. return (struct exynos_adc_data *)match->data;
  436. }
  437. static int exynos_read_raw(struct iio_dev *indio_dev,
  438. struct iio_chan_spec const *chan,
  439. int *val,
  440. int *val2,
  441. long mask)
  442. {
  443. struct exynos_adc *info = iio_priv(indio_dev);
  444. unsigned long timeout;
  445. int ret;
  446. if (mask == IIO_CHAN_INFO_SCALE) {
  447. ret = regulator_get_voltage(info->vdd);
  448. if (ret < 0)
  449. return ret;
  450. /* Regulator voltage is in uV, but need mV */
  451. *val = ret / 1000;
  452. *val2 = info->data->mask;
  453. return IIO_VAL_FRACTIONAL;
  454. } else if (mask != IIO_CHAN_INFO_RAW) {
  455. return -EINVAL;
  456. }
  457. mutex_lock(&info->lock);
  458. reinit_completion(&info->completion);
  459. /* Select the channel to be used and Trigger conversion */
  460. if (info->data->start_conv)
  461. info->data->start_conv(info, chan->address);
  462. timeout = wait_for_completion_timeout(&info->completion,
  463. EXYNOS_ADC_TIMEOUT);
  464. if (timeout == 0) {
  465. dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
  466. if (info->data->init_hw)
  467. info->data->init_hw(info);
  468. ret = -ETIMEDOUT;
  469. } else {
  470. *val = info->value;
  471. *val2 = 0;
  472. ret = IIO_VAL_INT;
  473. }
  474. mutex_unlock(&info->lock);
  475. return ret;
  476. }
  477. static int exynos_read_s3c64xx_ts(struct iio_dev *indio_dev, int *x, int *y)
  478. {
  479. struct exynos_adc *info = iio_priv(indio_dev);
  480. unsigned long timeout;
  481. int ret;
  482. mutex_lock(&info->lock);
  483. info->read_ts = true;
  484. reinit_completion(&info->completion);
  485. writel(ADC_S3C2410_TSC_PULL_UP_DISABLE | ADC_TSC_AUTOPST,
  486. ADC_V1_TSC(info->regs));
  487. /* Select the ts channel to be used and Trigger conversion */
  488. info->data->start_conv(info, ADC_S3C2410_MUX_TS);
  489. timeout = wait_for_completion_timeout(&info->completion,
  490. EXYNOS_ADC_TIMEOUT);
  491. if (timeout == 0) {
  492. dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
  493. if (info->data->init_hw)
  494. info->data->init_hw(info);
  495. ret = -ETIMEDOUT;
  496. } else {
  497. *x = info->ts_x;
  498. *y = info->ts_y;
  499. ret = 0;
  500. }
  501. info->read_ts = false;
  502. mutex_unlock(&info->lock);
  503. return ret;
  504. }
  505. static irqreturn_t exynos_adc_isr(int irq, void *dev_id)
  506. {
  507. struct exynos_adc *info = dev_id;
  508. u32 mask = info->data->mask;
  509. /* Read value */
  510. if (info->read_ts) {
  511. info->ts_x = readl(ADC_V1_DATX(info->regs));
  512. info->ts_y = readl(ADC_V1_DATY(info->regs));
  513. writel(ADC_TSC_WAIT4INT | ADC_S3C2443_TSC_UD_SEN, ADC_V1_TSC(info->regs));
  514. } else {
  515. info->value = readl(ADC_V1_DATX(info->regs)) & mask;
  516. }
  517. /* clear irq */
  518. if (info->data->clear_irq)
  519. info->data->clear_irq(info);
  520. complete(&info->completion);
  521. return IRQ_HANDLED;
  522. }
  523. /*
  524. * Here we (ab)use a threaded interrupt handler to stay running
  525. * for as long as the touchscreen remains pressed, we report
  526. * a new event with the latest data and then sleep until the
  527. * next timer tick. This mirrors the behavior of the old
  528. * driver, with much less code.
  529. */
  530. static irqreturn_t exynos_ts_isr(int irq, void *dev_id)
  531. {
  532. struct exynos_adc *info = dev_id;
  533. struct iio_dev *dev = dev_get_drvdata(info->dev);
  534. u32 x, y;
  535. bool pressed;
  536. int ret;
  537. while (READ_ONCE(info->ts_enabled)) {
  538. ret = exynos_read_s3c64xx_ts(dev, &x, &y);
  539. if (ret == -ETIMEDOUT)
  540. break;
  541. pressed = x & y & ADC_DATX_PRESSED;
  542. if (!pressed) {
  543. input_report_key(info->input, BTN_TOUCH, 0);
  544. input_sync(info->input);
  545. break;
  546. }
  547. input_report_abs(info->input, ABS_X, x & ADC_DATX_MASK);
  548. input_report_abs(info->input, ABS_Y, y & ADC_DATY_MASK);
  549. input_report_key(info->input, BTN_TOUCH, 1);
  550. input_sync(info->input);
  551. usleep_range(1000, 1100);
  552. }
  553. writel(0, ADC_V1_CLRINTPNDNUP(info->regs));
  554. return IRQ_HANDLED;
  555. }
  556. static int exynos_adc_reg_access(struct iio_dev *indio_dev,
  557. unsigned reg, unsigned writeval,
  558. unsigned *readval)
  559. {
  560. struct exynos_adc *info = iio_priv(indio_dev);
  561. if (readval == NULL)
  562. return -EINVAL;
  563. *readval = readl(info->regs + reg);
  564. return 0;
  565. }
  566. static const struct iio_info exynos_adc_iio_info = {
  567. .read_raw = &exynos_read_raw,
  568. .debugfs_reg_access = &exynos_adc_reg_access,
  569. };
  570. #define ADC_CHANNEL(_index, _id) { \
  571. .type = IIO_VOLTAGE, \
  572. .indexed = 1, \
  573. .channel = _index, \
  574. .address = _index, \
  575. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  576. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SCALE), \
  577. .datasheet_name = _id, \
  578. }
  579. static const struct iio_chan_spec exynos_adc_iio_channels[] = {
  580. ADC_CHANNEL(0, "adc0"),
  581. ADC_CHANNEL(1, "adc1"),
  582. ADC_CHANNEL(2, "adc2"),
  583. ADC_CHANNEL(3, "adc3"),
  584. ADC_CHANNEL(4, "adc4"),
  585. ADC_CHANNEL(5, "adc5"),
  586. ADC_CHANNEL(6, "adc6"),
  587. ADC_CHANNEL(7, "adc7"),
  588. ADC_CHANNEL(8, "adc8"),
  589. ADC_CHANNEL(9, "adc9"),
  590. };
  591. static int exynos_adc_remove_devices(struct device *dev, void *c)
  592. {
  593. struct platform_device *pdev = to_platform_device(dev);
  594. platform_device_unregister(pdev);
  595. return 0;
  596. }
  597. static int exynos_adc_ts_open(struct input_dev *dev)
  598. {
  599. struct exynos_adc *info = input_get_drvdata(dev);
  600. WRITE_ONCE(info->ts_enabled, true);
  601. enable_irq(info->tsirq);
  602. return 0;
  603. }
  604. static void exynos_adc_ts_close(struct input_dev *dev)
  605. {
  606. struct exynos_adc *info = input_get_drvdata(dev);
  607. WRITE_ONCE(info->ts_enabled, false);
  608. disable_irq(info->tsirq);
  609. }
  610. static int exynos_adc_ts_init(struct exynos_adc *info)
  611. {
  612. int ret;
  613. if (info->tsirq <= 0)
  614. return -ENODEV;
  615. info->input = input_allocate_device();
  616. if (!info->input)
  617. return -ENOMEM;
  618. info->input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  619. info->input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  620. input_set_abs_params(info->input, ABS_X, 0, 0x3FF, 0, 0);
  621. input_set_abs_params(info->input, ABS_Y, 0, 0x3FF, 0, 0);
  622. info->input->name = "S3C24xx TouchScreen";
  623. info->input->id.bustype = BUS_HOST;
  624. info->input->open = exynos_adc_ts_open;
  625. info->input->close = exynos_adc_ts_close;
  626. input_set_drvdata(info->input, info);
  627. ret = input_register_device(info->input);
  628. if (ret) {
  629. input_free_device(info->input);
  630. return ret;
  631. }
  632. ret = request_threaded_irq(info->tsirq, NULL, exynos_ts_isr,
  633. IRQF_ONESHOT | IRQF_NO_AUTOEN,
  634. "touchscreen", info);
  635. if (ret)
  636. input_unregister_device(info->input);
  637. return ret;
  638. }
  639. static int exynos_adc_probe(struct platform_device *pdev)
  640. {
  641. struct exynos_adc *info = NULL;
  642. struct device_node *np = pdev->dev.of_node;
  643. struct s3c2410_ts_mach_info *pdata = dev_get_platdata(&pdev->dev);
  644. struct iio_dev *indio_dev = NULL;
  645. bool has_ts = false;
  646. int ret;
  647. int irq;
  648. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct exynos_adc));
  649. if (!indio_dev) {
  650. dev_err(&pdev->dev, "failed allocating iio device\n");
  651. return -ENOMEM;
  652. }
  653. info = iio_priv(indio_dev);
  654. info->data = exynos_adc_get_data(pdev);
  655. if (!info->data) {
  656. dev_err(&pdev->dev, "failed getting exynos_adc_data\n");
  657. return -EINVAL;
  658. }
  659. info->regs = devm_platform_ioremap_resource(pdev, 0);
  660. if (IS_ERR(info->regs))
  661. return PTR_ERR(info->regs);
  662. if (info->data->needs_adc_phy) {
  663. info->pmu_map = syscon_regmap_lookup_by_phandle(
  664. pdev->dev.of_node,
  665. "samsung,syscon-phandle");
  666. if (IS_ERR(info->pmu_map)) {
  667. dev_err(&pdev->dev, "syscon regmap lookup failed.\n");
  668. return PTR_ERR(info->pmu_map);
  669. }
  670. }
  671. /* leave out any TS related code if unreachable */
  672. if (IS_REACHABLE(CONFIG_INPUT)) {
  673. has_ts = of_property_read_bool(pdev->dev.of_node,
  674. "has-touchscreen") || pdata;
  675. }
  676. irq = platform_get_irq(pdev, 0);
  677. if (irq < 0)
  678. return irq;
  679. info->irq = irq;
  680. if (has_ts) {
  681. irq = platform_get_irq(pdev, 1);
  682. if (irq == -EPROBE_DEFER)
  683. return irq;
  684. info->tsirq = irq;
  685. } else {
  686. info->tsirq = -1;
  687. }
  688. info->dev = &pdev->dev;
  689. init_completion(&info->completion);
  690. info->clk = devm_clk_get(&pdev->dev, "adc");
  691. if (IS_ERR(info->clk)) {
  692. dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
  693. PTR_ERR(info->clk));
  694. return PTR_ERR(info->clk);
  695. }
  696. if (info->data->needs_sclk) {
  697. info->sclk = devm_clk_get(&pdev->dev, "sclk");
  698. if (IS_ERR(info->sclk)) {
  699. dev_err(&pdev->dev,
  700. "failed getting sclk clock, err = %ld\n",
  701. PTR_ERR(info->sclk));
  702. return PTR_ERR(info->sclk);
  703. }
  704. }
  705. info->vdd = devm_regulator_get(&pdev->dev, "vdd");
  706. if (IS_ERR(info->vdd))
  707. return dev_err_probe(&pdev->dev, PTR_ERR(info->vdd),
  708. "failed getting regulator");
  709. ret = regulator_enable(info->vdd);
  710. if (ret)
  711. return ret;
  712. ret = exynos_adc_prepare_clk(info);
  713. if (ret)
  714. goto err_disable_reg;
  715. ret = exynos_adc_enable_clk(info);
  716. if (ret)
  717. goto err_unprepare_clk;
  718. platform_set_drvdata(pdev, indio_dev);
  719. indio_dev->name = dev_name(&pdev->dev);
  720. indio_dev->info = &exynos_adc_iio_info;
  721. indio_dev->modes = INDIO_DIRECT_MODE;
  722. indio_dev->channels = exynos_adc_iio_channels;
  723. indio_dev->num_channels = info->data->num_channels;
  724. mutex_init(&info->lock);
  725. ret = request_irq(info->irq, exynos_adc_isr,
  726. 0, dev_name(&pdev->dev), info);
  727. if (ret < 0) {
  728. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n",
  729. info->irq);
  730. goto err_disable_clk;
  731. }
  732. ret = iio_device_register(indio_dev);
  733. if (ret)
  734. goto err_irq;
  735. if (info->data->init_hw)
  736. info->data->init_hw(info);
  737. if (pdata)
  738. info->delay = pdata->delay;
  739. else
  740. info->delay = 10000;
  741. if (has_ts)
  742. ret = exynos_adc_ts_init(info);
  743. if (ret)
  744. goto err_iio;
  745. ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev);
  746. if (ret < 0) {
  747. dev_err(&pdev->dev, "failed adding child nodes\n");
  748. goto err_of_populate;
  749. }
  750. return 0;
  751. err_of_populate:
  752. device_for_each_child(&indio_dev->dev, NULL,
  753. exynos_adc_remove_devices);
  754. if (has_ts) {
  755. input_unregister_device(info->input);
  756. free_irq(info->tsirq, info);
  757. }
  758. err_iio:
  759. iio_device_unregister(indio_dev);
  760. err_irq:
  761. free_irq(info->irq, info);
  762. err_disable_clk:
  763. if (info->data->exit_hw)
  764. info->data->exit_hw(info);
  765. exynos_adc_disable_clk(info);
  766. err_unprepare_clk:
  767. exynos_adc_unprepare_clk(info);
  768. err_disable_reg:
  769. regulator_disable(info->vdd);
  770. return ret;
  771. }
  772. static int exynos_adc_remove(struct platform_device *pdev)
  773. {
  774. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  775. struct exynos_adc *info = iio_priv(indio_dev);
  776. if (IS_REACHABLE(CONFIG_INPUT) && info->input) {
  777. free_irq(info->tsirq, info);
  778. input_unregister_device(info->input);
  779. }
  780. device_for_each_child(&indio_dev->dev, NULL,
  781. exynos_adc_remove_devices);
  782. iio_device_unregister(indio_dev);
  783. free_irq(info->irq, info);
  784. if (info->data->exit_hw)
  785. info->data->exit_hw(info);
  786. exynos_adc_disable_clk(info);
  787. exynos_adc_unprepare_clk(info);
  788. regulator_disable(info->vdd);
  789. return 0;
  790. }
  791. static int exynos_adc_suspend(struct device *dev)
  792. {
  793. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  794. struct exynos_adc *info = iio_priv(indio_dev);
  795. if (info->data->exit_hw)
  796. info->data->exit_hw(info);
  797. exynos_adc_disable_clk(info);
  798. regulator_disable(info->vdd);
  799. return 0;
  800. }
  801. static int exynos_adc_resume(struct device *dev)
  802. {
  803. struct iio_dev *indio_dev = dev_get_drvdata(dev);
  804. struct exynos_adc *info = iio_priv(indio_dev);
  805. int ret;
  806. ret = regulator_enable(info->vdd);
  807. if (ret)
  808. return ret;
  809. ret = exynos_adc_enable_clk(info);
  810. if (ret)
  811. return ret;
  812. if (info->data->init_hw)
  813. info->data->init_hw(info);
  814. return 0;
  815. }
  816. static DEFINE_SIMPLE_DEV_PM_OPS(exynos_adc_pm_ops, exynos_adc_suspend,
  817. exynos_adc_resume);
  818. static struct platform_driver exynos_adc_driver = {
  819. .probe = exynos_adc_probe,
  820. .remove = exynos_adc_remove,
  821. .driver = {
  822. .name = "exynos-adc",
  823. .of_match_table = exynos_adc_match,
  824. .pm = pm_sleep_ptr(&exynos_adc_pm_ops),
  825. },
  826. };
  827. module_platform_driver(exynos_adc_driver);
  828. MODULE_AUTHOR("Naveen Krishna Chatradhi <[email protected]>");
  829. MODULE_DESCRIPTION("Samsung EXYNOS5 ADC driver");
  830. MODULE_LICENSE("GPL v2");