exynos_adc.c 25 KB

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