exynos_adc.c 24 KB

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