ark-adc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/platform_device.h>
  4. #include <linux/io.h>
  5. #include <linux/iio/iio.h>
  6. #include <linux/iio/machine.h>
  7. #include <linux/iio/driver.h>
  8. #include <linux/mfd/syscon.h>
  9. #include <linux/regmap.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/err.h>
  12. #include <linux/errno.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/delay.h>
  15. #include <linux/clk.h>
  16. #include <linux/completion.h>
  17. enum ADC_MODE{
  18. ARK_ADC_MODE_NONE,
  19. ARK_ADC_MODE_BATTERY,
  20. ARK_ADC_MODE_KEY,
  21. ARK_ADC_MODE_END,
  22. };
  23. struct ark_adc {
  24. struct ark_adc_data *data;
  25. struct device *dev;
  26. void __iomem *sys_base;
  27. void __iomem *adc_base;
  28. struct clk *clk;
  29. struct completion completion;
  30. u32 irq;
  31. u32 mode[4]; //adc channel[n] mode(battery, key, ...)
  32. u32 value1[4]; //adc channel[n] temp value
  33. u32 value2[4]; //adc channel[n] final value
  34. };
  35. struct ark_adc_data {
  36. int num_channels;
  37. u32 mask;
  38. void (*init_hw)(struct ark_adc *info);
  39. void (*exit_hw)(struct ark_adc *info);
  40. //void (*clear_irq)(struct ark_adc *info);
  41. void (*start_conv)(struct ark_adc *info, unsigned long addr);
  42. };
  43. #define BAT_CHANNEL 1
  44. #define TOUCHPAN_CHANNEL 2
  45. #define AUX0_CHANNEL 3
  46. #define AUX1_CHANNEL 4
  47. #define AUX2_CHANNEL 5
  48. #define AUX3_CHANNEL 6
  49. #define AUX0_START_INT (1<<0)
  50. #define AUX0_STOP_INT (1<<1)
  51. #define AUX0_VALUE_INT (1<<2)
  52. #define AUX1_START_INT (1<<3)
  53. #define AUX1_STOP_INT (1<<4)
  54. #define AUX1_VALUE_INT (1<<5)
  55. #define AUX2_START_INT (1<<6)
  56. #define AUX2_STOP_INT (1<<7)
  57. #define AUX2_VALUE_INT (1<<8)
  58. #define AUX3_START_INT (1<<9)
  59. #define AUX3_STOP_INT (1<<10)
  60. #define AUX3_VALUE_INT (1<<11)
  61. #define AUX_START_INT(ch) (1 << ((ch)*3 + 0))
  62. #define AUX_STOP_INT(ch) (1 << ((ch)*3 + 1))
  63. #define AUX_VALUE_INT(ch) (1 << ((ch)*3 + 2))
  64. #define ARK1668_ADC_CHANNEL_MAX_NUMS 4
  65. #define ARKN141_ADC_CHANNEL_MAX_NUMS 2
  66. #define ARK_ADC_DATX_MASK_12BIT 0xFFF
  67. #define ARK_ADC_DATX_MASK_10BIT 0x3FF
  68. #define ARK_BATTERY_VOLTAGE_REF 3300// 3.3V
  69. #define ARK_ADC_TIMEOUT (msecs_to_jiffies(100))
  70. #define ARK_ADC_USE_PULL_UP_REGISTER //ADC connect pull up register, have interrupe when voltage in 0V ~ 3V
  71. //#define ARK_ADC_USE_PULL_DOWM_REGISTER //ADC connect pull dowm register, have interrupt when voltage in 0.3V ~ 3.3V
  72. #define ADC_CHANNEL(_index, _id) { \
  73. .type = IIO_VOLTAGE, \
  74. .indexed = 1, \
  75. .channel = _index, \
  76. .address = _index, \
  77. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  78. .datasheet_name = _id, \
  79. }
  80. static const struct iio_chan_spec ark_adc_iio_channels[] = {
  81. ADC_CHANNEL(0, "adc0"),
  82. ADC_CHANNEL(1, "adc1"),
  83. ADC_CHANNEL(2, "adc2"),
  84. ADC_CHANNEL(3, "adc3"),
  85. };
  86. static int ark_adc_reg_access(struct iio_dev *indio_dev,
  87. unsigned reg, unsigned writeval,
  88. unsigned *readval)
  89. {
  90. //struct ark_adc *info = iio_priv(indio_dev);
  91. //printk(KERN_ALERT "### ark_adc_reg_access\n");
  92. if (!readval)
  93. return -EINVAL;
  94. //*readval = readl(info->adc_base + reg);
  95. return 0;
  96. }
  97. static int ark_read_raw(struct iio_dev *indio_dev,
  98. struct iio_chan_spec const *chan,
  99. int *val,
  100. int *val2,
  101. long mask)
  102. {
  103. struct ark_adc *info = iio_priv(indio_dev);
  104. unsigned long timeout;
  105. int ret = 0;
  106. mutex_lock(&indio_dev->mlock);
  107. reinit_completion(&info->completion);
  108. /* Select the channel to be used and Trigger conversion */
  109. if (info->data->start_conv)
  110. info->data->start_conv(info, chan->address);
  111. timeout = wait_for_completion_timeout(&info->completion, ARK_ADC_TIMEOUT);
  112. if (timeout == 0) {
  113. dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n");
  114. if (info->data->init_hw)
  115. info->data->init_hw(info);
  116. ret = -ETIMEDOUT;
  117. } else {
  118. *val = info->value2[chan->address];
  119. *val2 = 0;
  120. ret = IIO_VAL_INT;
  121. }
  122. mutex_unlock(&indio_dev->mlock);
  123. //printk(KERN_ALERT "### READ :%d\n", readl(info->adc_base+0x14));
  124. return ret;
  125. }
  126. static const struct iio_info ark_adc_iio_info = {
  127. .read_raw = &ark_read_raw,
  128. .debugfs_reg_access = &ark_adc_reg_access,
  129. };
  130. static void arn141_adc_set_transform_interval(struct ark_adc *info, u32 interval_millisecond)
  131. {
  132. u32 reg;
  133. // 1M Hz sample clock
  134. // adc_clk = clk_24m/((adc_clk_div+1)*2).
  135. reg = readl(info->sys_base+0x64);
  136. reg &= ~0x7FFF;
  137. reg |= (12 - 1);
  138. writel(reg, info->sys_base+0x64);
  139. reg = interval_millisecond * 1000; // 转换为微秒计数
  140. if(reg > 0xFFFF)
  141. reg = 0xFFFF;
  142. writel(reg, info->adc_base+0x30);
  143. }
  144. static u32 arkn141_get_cpu_pll_freq(struct ark_adc *info)
  145. {
  146. u32 val;
  147. u32 ref_clk;
  148. u32 no,nf,enable;
  149. u32 speed;
  150. speed = 0;
  151. val = readl(info->sys_base+0x14c);
  152. ref_clk = 12000000 * ((val & 0x1));
  153. val = readl(info->sys_base+0x150);
  154. no = (val >> 12) & 0x03;
  155. if(no == 0)
  156. no = 1;
  157. else if(no == 1)
  158. no = 2;
  159. else if(no == 2)
  160. no = 4;
  161. else
  162. no = 8;
  163. nf = (val & 0xFF)+1;
  164. enable = (val >> 14) & 0x1;
  165. if(enable)
  166. speed = ref_clk * nf /no;
  167. //printk(KERN_ALERT "### CPUPLL=%d\n", speed);
  168. return speed;
  169. }
  170. static u32 arkn141_get_sys_pll_freq(struct ark_adc *info)
  171. {
  172. u32 val;
  173. u32 ref_clk;
  174. u32 no,nf,enable;
  175. u32 speed;
  176. speed = 0;
  177. val = readl(info->sys_base+0x14c);
  178. ref_clk = 12000000 * (((val>>3) & 0x1));
  179. val = readl(info->sys_base+0x154);
  180. no = (val >> 12) & 0x03;
  181. if(no == 0)
  182. no = 1;
  183. else if(no == 1)
  184. no = 2;
  185. else if(no == 2)
  186. no = 4;
  187. else
  188. no = 8;
  189. nf = (val & 0xFF)+1;
  190. enable = (val >> 14) & 0x1;
  191. if(enable)
  192. speed = ref_clk * nf /no;
  193. //printk(KERN_ALERT "### SYSPLL=%d\n", speed);
  194. return speed;
  195. }
  196. static u32 arkn141_get_aud_pll_freq(struct ark_adc *info)
  197. {
  198. u32 val;
  199. u32 ref_clk;
  200. u32 no,nf,enable;
  201. u32 speed;
  202. speed = 0;
  203. val = readl(info->sys_base+0x14c);
  204. ref_clk = 12000000 * (((val>>6)&0x1));
  205. val = readl(info->sys_base+0x158);
  206. no = (val >> 12) & 0x03;
  207. if(no == 0)
  208. no = 1;
  209. else if(no == 1)
  210. no = 2;
  211. else if(no == 2)
  212. no = 4;
  213. else
  214. no = 8;
  215. nf = val & 0xFF;
  216. enable = (val >> 14) & 0x1;
  217. if(enable)
  218. speed = ref_clk * nf /no;
  219. //printk(KERN_ALERT "### AUDPLL=%d\n", speed);
  220. return speed;
  221. }
  222. static u32 arkn141_get_ext24_excrypt_freq(struct ark_adc *info)
  223. {
  224. //printk(KERN_ALERT "### 24M\n");
  225. return 24000000;
  226. }
  227. static u32 arkn141_get_clk(struct ark_adc *info)
  228. {
  229. u32 val;
  230. u32 main_hclk_source;
  231. u32 hclk_div;
  232. u32 main_hclk;
  233. u32 speed;
  234. val = readl(info->sys_base+0x40);
  235. // main_hclk_clk_sel
  236. // SYS_CLK_SEL [17-15]
  237. // 3b000 : cpupll_clk
  238. // 3b001 : syspll_clk
  239. // 3b010 : audpll_clk
  240. // 3b100 : clk_24m
  241. main_hclk_source = (val >> 15) & 0x07;
  242. // hclk_div
  243. // SYS_CLK_SEL [21-19]
  244. // hclk = main_hclk / (hclk_div + 1)
  245. hclk_div = (val >> 19) & 0x07;
  246. hclk_div = hclk_div + 1;
  247. switch(main_hclk_source)
  248. {
  249. case 0:
  250. main_hclk = arkn141_get_cpu_pll_freq(info);
  251. break;
  252. case 1:
  253. main_hclk = arkn141_get_sys_pll_freq(info);
  254. break;
  255. case 2:
  256. main_hclk = arkn141_get_aud_pll_freq(info);
  257. break;
  258. case 4:
  259. main_hclk = arkn141_get_ext24_excrypt_freq(info);
  260. break;
  261. default :
  262. main_hclk = 0;
  263. break;
  264. }
  265. speed = main_hclk / hclk_div;
  266. return speed;
  267. }
  268. static u32 arkn141_adc_get_apb_clk(struct ark_adc *info)
  269. {
  270. u32 val;
  271. u32 hclk;
  272. u32 pclk_div;
  273. u32 pclk;
  274. val = readl(info->sys_base+0x40);
  275. // pclk_div
  276. // SYS_CLK_SEL [24-23]
  277. // 2'b00: pclk = hclk
  278. // 2'b01: pclk = hclk/2
  279. // 2'b10: pclk = hclk/4
  280. pclk_div = (val >> 23) & 0x3;
  281. hclk = arkn141_get_clk(info);
  282. pclk = hclk / ( 1<< pclk_div);
  283. return pclk;
  284. }
  285. static void arkn141_adc_set_debounce_time(struct ark_adc *info, u32 debounce_time_us)
  286. {
  287. u32 dbcount;
  288. dbcount = (debounce_time_us * arkn141_adc_get_apb_clk(info) / 1000000) ;
  289. if(dbcount >= 0xFFFFF)
  290. dbcount = 0xFFFFF;
  291. writel(dbcount, info->adc_base+0x2c);
  292. }
  293. static void arkn141_adc_soft_reset(struct ark_adc *info)
  294. {
  295. unsigned int val;
  296. val = readl(info->sys_base+0x74);
  297. val &= ~(1 << 18);
  298. writel(val, info->sys_base+0x74);
  299. msleep (1);
  300. val = readl(info->sys_base+0x74);
  301. val |= (1 << 18);
  302. writel(val, info->sys_base+0x74);
  303. }
  304. static void arkn141_adc_set_clk(struct ark_adc *info, u32 enable)
  305. {
  306. u32 val;
  307. if(enable)
  308. {
  309. //enable pclk
  310. val = readl(info->sys_base+0x48);
  311. val |= (1 << 3);
  312. writel(val, info->sys_base+0x48);
  313. //enable clk
  314. val = readl(info->sys_base+0x50);
  315. val |= (1 << 16);
  316. writel(val, info->sys_base+0x50);
  317. }
  318. else
  319. {
  320. //disable pclk
  321. val = readl(info->sys_base+0x48);
  322. val &= ~(1 << 3);
  323. writel(val, info->sys_base+0x48);
  324. //disable clk
  325. val = readl(info->sys_base+0x50);
  326. val &= ~(1 << 16);
  327. writel(val, info->sys_base+0x50);
  328. }
  329. }
  330. static void arkn141_enable_adc_channel(struct ark_adc *info, u32 channel)
  331. {
  332. u32 value;
  333. switch(channel)
  334. {
  335. case AUX0_CHANNEL: //battery voltage detect
  336. {
  337. //clear interrupt state
  338. value = readl(info->adc_base+0x0c);
  339. value &= ~((1<<2)|(1<<1)|(1<<0));
  340. writel(value, info->adc_base+0x0c);
  341. //mask start/stop interrupt
  342. value = readl(info->adc_base+0x08);
  343. value &= ~((1<<2)|(1<<1)|(1<<0));
  344. //value |= ((1<<2)|(1<<1)|(1<<0)); //mask start/stop interrupt
  345. writel(value, info->adc_base+0x08);
  346. //enable channel
  347. value = readl(info->adc_base+0x0);
  348. value |= (1<<AUX0_CHANNEL);
  349. writel(value, info->adc_base+0x0);
  350. break;
  351. }
  352. case AUX1_CHANNEL: // key
  353. {
  354. //enable channel
  355. value = readl(info->adc_base+0x0);
  356. value |= (1<<AUX1_CHANNEL);
  357. writel(value, info->adc_base+0x0);
  358. //mask start/stop interrupt
  359. value = readl(info->adc_base+0x08);
  360. value &= ~((1<<5)|(1<<4)|(1<<3));
  361. //value &= ~((1<<5)|(1<<4)); //not use start
  362. writel(value, info->adc_base+0x08);
  363. break;
  364. }
  365. default:
  366. break;
  367. }
  368. }
  369. static void arkn141_adc_init_hw(struct ark_adc *info)
  370. {
  371. unsigned int val;
  372. arkn141_adc_set_clk(info, 0);
  373. msleep(1);
  374. arkn141_adc_soft_reset(info);
  375. msleep(1);
  376. arkn141_adc_set_clk(info, 1);
  377. msleep(1);
  378. //set referance voltage
  379. val = readl(info->sys_base+0x140);
  380. val &= ~(1 << 22);
  381. val |= (1 << 21);
  382. writel(val, info->sys_base+0x140);
  383. //reset adc module
  384. val = readl(info->adc_base+0x0);
  385. val |= (1 << 0);
  386. writel(val, info->adc_base+0x0);
  387. msleep(1);
  388. //val = readl(info->sys_base+0x0);
  389. val = 0;
  390. writel(val, info->adc_base+0x0);
  391. //KEY set debounce time 100us
  392. arkn141_adc_set_debounce_time (info, 100);
  393. //set adc transform interval
  394. arn141_adc_set_transform_interval(info, 16);
  395. //enable adc interrupt
  396. val = (0x07 << 0) | (0x07 << 3); //AUX0(BAT) | AUX1(KEY)
  397. writel(val, info->adc_base+0x08);
  398. //adc control register
  399. val = 0;
  400. writel(val, info->adc_base+0x0);
  401. //clear all intertupt
  402. val = 0; //bit clear
  403. writel(val, info->adc_base+0x0c);
  404. // sch default is high , key press is low
  405. val = readl(info->sys_base+0x144);
  406. #ifdef ARK_ADC_USE_PULL_UP_REGISTER
  407. val &= ~(1 << 14); // 0: connect a pull-up resister (3.0v - 0v have interrupt)
  408. #else
  409. val |= (1 << 14); // 1: connect a pull-down resister (0.3v - 3.3v have interrupt) -- based on hard ware
  410. #endif
  411. writel(val, info->sys_base+0x144);
  412. //set detect level
  413. val = readl(info->adc_base+0x0);
  414. val |= (1<<8); // 1: aux0_det high valid.
  415. writel(val, info->adc_base+0x0);
  416. val = readl(info->adc_base+0x0);
  417. val |= (1<<9); // 1: aux1_det high valid.
  418. writel(val, info->adc_base+0x0);
  419. //arkn141_enable_adc_channel(info, AUX1_CHANNEL); // 按键
  420. arkn141_enable_adc_channel(info, AUX0_CHANNEL); // 电压检测
  421. }
  422. static void arkn141_adc_exit_hw(struct ark_adc *info)
  423. {
  424. //printk(KERN_ALERT "### exit\n");
  425. }
  426. static void arkn141_adc_start_conv(struct ark_adc *info, unsigned long addr)
  427. {
  428. u32 value = info->value1[addr];
  429. #ifdef ARK_ADC_USE_PULL_UP_REGISTER
  430. if((value >= 0) && (value <= 3000))
  431. info->value2[addr] = value;
  432. else
  433. info->value2[addr] = ARK_BATTERY_VOLTAGE_REF;
  434. info->value1[addr] = ARK_BATTERY_VOLTAGE_REF;
  435. #else
  436. info->value2[addr] = value;
  437. #endif
  438. complete(&info->completion);
  439. }
  440. static void ark1668_adc_init_hw(struct ark_adc *info)
  441. {
  442. //ADC pclk disable
  443. }
  444. static void ark1668_adc_exit_hw(struct ark_adc *info)
  445. {
  446. }
  447. static void ark1668_adc_start_conv(struct ark_adc *info, unsigned long addr)
  448. {
  449. }
  450. static const struct ark_adc_data arkn141_adc_data = {
  451. .num_channels = ARKN141_ADC_CHANNEL_MAX_NUMS,
  452. .mask = ARK_ADC_DATX_MASK_12BIT,
  453. .init_hw = arkn141_adc_init_hw,
  454. .exit_hw = arkn141_adc_exit_hw,
  455. .start_conv = arkn141_adc_start_conv,
  456. };
  457. static const struct ark_adc_data ark1668_adc_data = {
  458. .num_channels = ARK1668_ADC_CHANNEL_MAX_NUMS,
  459. .mask = ARK_ADC_DATX_MASK_12BIT,
  460. .init_hw = ark1668_adc_init_hw,
  461. .exit_hw = ark1668_adc_exit_hw,
  462. .start_conv = ark1668_adc_start_conv,
  463. };
  464. static const struct of_device_id ark_adc_match[] = {
  465. {
  466. .compatible = "arkmicro,ark1668-adc",
  467. .data = &ark1668_adc_data,
  468. },
  469. {
  470. .compatible = "arkmicro,arkn141-adc",
  471. .data = &arkn141_adc_data,
  472. },
  473. {},
  474. };
  475. MODULE_DEVICE_TABLE(of, ark_adc_match);
  476. static struct ark_adc_data *ark_adc_get_data(struct platform_device *pdev)
  477. {
  478. const struct of_device_id *match = of_match_node(ark_adc_match, pdev->dev.of_node);
  479. return (struct ark_adc_data *)match->data;
  480. }
  481. static irqreturn_t ark_adc_intr_handler(int irq, void *dev_id)
  482. {
  483. struct ark_adc *info = (struct ark_adc *)dev_id;
  484. u32 value,status;
  485. #if 1
  486. status = readl(info->adc_base+0x0c);
  487. //AUX0(BAT)
  488. if(status & AUX0_START_INT)
  489. {
  490. value = readl(info->adc_base+0x0c);
  491. value &= ~AUX0_START_INT;
  492. writel(value, info->adc_base+0x0c);
  493. }
  494. if(status & AUX0_VALUE_INT)
  495. {
  496. u32 range,val;
  497. value = readl(info->adc_base+0x14);
  498. val = readl(info->adc_base+0x0c);
  499. val &= ~AUX0_VALUE_INT;
  500. writel(val, info->adc_base+0x0c);
  501. //range = ((info->data->mask==ARK_ADC_DATX_MASK_12BIT) ? 4096 : 1024);
  502. range = ((info->data->mask==ARK_ADC_DATX_MASK_12BIT) ? 4095 : 1023);
  503. info->value1[0] = value * ARK_BATTERY_VOLTAGE_REF / range;
  504. //printk(KERN_ALERT "AUX0### value:%d,info->value:%d\n",value,info->value1[0]);
  505. }
  506. if(status & AUX0_STOP_INT)
  507. {
  508. value = readl(info->adc_base+0x0c);
  509. value &= ~AUX0_STOP_INT;
  510. writel(value, info->adc_base+0x0c);
  511. }
  512. //AUX1(KEY)
  513. if(status & AUX1_START_INT)
  514. {
  515. value = readl(info->adc_base+0x0c);
  516. value &= ~AUX1_START_INT;
  517. writel(value, info->adc_base+0x0c);
  518. // 关闭AUX0. ADC是互斥资源, 只允许一个AUX使用.
  519. // 当AUX1有需要转换的数据时(按键电压值转换), 将AUX0关闭, 分配ADC资源给AUX1使用.
  520. // AUX1转换完毕后(STOP_INT), 重新使能AUX0
  521. //close AUX0, because only one channel can be used once a time.
  522. //when AUX1 working (key value convert to voltage value), we must close AUX0, ADC distribute resource to AUX1.
  523. //when AUX1 convert over, enable AUX0 again.
  524. value = readl(info->adc_base+0x0);
  525. value &= ~(1 << AUX0_CHANNEL);
  526. writel(value, info->adc_base+0x0);
  527. }
  528. if(status & AUX1_VALUE_INT)
  529. {
  530. u32 aux1_range,aux1_val;
  531. value =readl(info->adc_base+0x18);; // 读取ADC采样值
  532. aux1_val = readl(info->adc_base+0x0c);
  533. aux1_val &= ~AUX1_VALUE_INT;
  534. writel(aux1_val, info->adc_base+0x0c);
  535. aux1_range = ((info->data->mask==ARK_ADC_DATX_MASK_12BIT) ? 4095 : 1023);
  536. info->value1[1] = value * ARK_BATTERY_VOLTAGE_REF / aux1_range;
  537. //printk(KERN_ALERT "AUX1### value:%d,info->value:%d\n",value,info->value1[1]);
  538. }
  539. if((status & AUX1_STOP_INT))
  540. {
  541. // 按键释放
  542. value = readl(info->adc_base+0x0c);
  543. value &= ~AUX1_STOP_INT;
  544. writel(value, info->adc_base+0x0c);
  545. // 使用完毕后, 重新允许AUX0. AUX0可以继续采样电压值.
  546. value = readl(info->adc_base+0x0);
  547. value |= (1 << AUX0_CHANNEL);
  548. writel(value, info->adc_base+0x0);
  549. }
  550. #else
  551. //解决按键检测时影响电压检测值问题 ,针对成都N141 钓鱼板项目
  552. int aux0_start = 0;
  553. int aux1_start = 0;
  554. static int key_count = 0;
  555. status = readl(info->adc_base+0x0c);
  556. //printk(KERN_ALERT ">>>>>>>>>>>>>>>>>>>>status:%d<<<<<<<<<<<<<<<<<<<<<<<\n",status);
  557. //AUX0(BAT)
  558. if((status & AUX0_START_INT))
  559. {//printk(KERN_ALERT "AUX0_START_INT++++++++++++++++++++++++++++++++++++++\n");
  560. value = readl(info->adc_base+0x0c);
  561. value &= ~AUX0_START_INT;
  562. writel(value, info->adc_base+0x0c);
  563. }
  564. if(status & AUX0_VALUE_INT)
  565. {
  566. u32 range,val;
  567. value = readl(info->adc_base+0x14);
  568. val = readl(info->adc_base+0x0c);
  569. val &= ~AUX0_VALUE_INT;
  570. writel(val, info->adc_base+0x0c);
  571. //range = ((info->data->mask==ARK_ADC_DATX_MASK_12BIT) ? 4096 : 1024);
  572. range = ((info->data->mask==ARK_ADC_DATX_MASK_12BIT) ? 4095 : 1023);
  573. info->value1[0] = value * ARK_BATTERY_VOLTAGE_REF / range;
  574. printk(KERN_ALERT "AUX0### value:%d,info->value:%d\n",value,info->value1[0]);
  575. aux0_start = 1;
  576. }
  577. if(status & AUX0_STOP_INT)
  578. {//printk(KERN_ALERT "AUX0_STOP_INT----------------------------------------------\n");
  579. value = readl(info->adc_base+0x0c);
  580. value &= ~AUX0_STOP_INT;
  581. writel(value, info->adc_base+0x0c);
  582. }
  583. //AUX1(KEY)
  584. if((status & AUX1_START_INT))
  585. {//printk(KERN_ALERT "AUX1_START_INT++++++++++++++++++++++++++++++++++++++\n");
  586. value = readl(info->adc_base+0x0c);
  587. value &= ~AUX1_START_INT;
  588. writel(value, info->adc_base+0x0c);
  589. // 关闭AUX0. ADC是互斥资源, 只允许一个AUX使用.
  590. // 当AUX1有需要转换的数据时(按键电压值转换), 将AUX0关闭, 分配ADC资源给AUX1使用.
  591. // AUX1转换完毕后(STOP_INT), 重新使能AUX0
  592. //close AUX0, because only one channel can be used once a time.
  593. //when AUX1 working (key value convert to voltage value), we must close AUX0, ADC distribute resource to AUX1.
  594. //when AUX1 convert over, enable AUX0 again.
  595. value = readl(info->adc_base+0x0);
  596. value &= ~(1 << AUX0_CHANNEL);
  597. writel(value, info->adc_base+0x0);
  598. key_count = 1;
  599. }
  600. if(key_count >1)
  601. {
  602. value = readl(info->adc_base+0x0c);
  603. value &= ~AUX1_START_INT;
  604. writel(value, info->adc_base+0x0c);
  605. value = readl(info->adc_base+0x0);
  606. value &= ~(1 << AUX0_CHANNEL);
  607. writel(value, info->adc_base+0x0);
  608. key_count = 1;
  609. }
  610. if(status & AUX1_VALUE_INT)
  611. {
  612. u32 aux1_range,aux1_val;
  613. value =readl(info->adc_base+0x18);; // 读取ADC采样值
  614. aux1_val = readl(info->adc_base+0x0c);
  615. aux1_val &= ~AUX1_VALUE_INT;
  616. writel(aux1_val, info->adc_base+0x0c);
  617. aux1_range = ((info->data->mask==ARK_ADC_DATX_MASK_12BIT) ? 4095 : 1023);
  618. info->value1[1] = value * ARK_BATTERY_VOLTAGE_REF / aux1_range;
  619. printk(KERN_ALERT "AUX1### value:%d,info->value:%d\n",value,info->value1[1]);
  620. //aux1_start = 1;
  621. key_count ++;
  622. }
  623. if((status & AUX1_STOP_INT))
  624. {//printk(KERN_ALERT "AUX1_STOP_INT----------------------------------------------\n");
  625. // 按键释放
  626. value = readl(info->adc_base+0x0c);
  627. value &= ~AUX1_STOP_INT;
  628. writel(value, info->adc_base+0x0c);
  629. // 使用完毕后, 重新允许AUX0. AUX0可以继续采样电压值.
  630. value = readl(info->adc_base+0x0);
  631. value |= (1 << AUX0_CHANNEL);
  632. writel(value, info->adc_base+0x0);
  633. key_count = 0;
  634. }
  635. if(key_count >1)
  636. {
  637. // 按键释放
  638. value = readl(info->adc_base+0x0c);
  639. value &= ~AUX1_STOP_INT;
  640. writel(value, info->adc_base+0x0c);
  641. // 使用完毕后, 重新允许AUX0. AUX0可以继续采样电压值.
  642. value = readl(info->adc_base+0x0);
  643. value |= (1 << AUX0_CHANNEL);
  644. writel(value, info->adc_base+0x0);
  645. }
  646. #endif
  647. return IRQ_HANDLED;
  648. }
  649. static int ark_adc_probe(struct platform_device *pdev)
  650. {
  651. //struct device_node *np = pdev->dev.of_node;
  652. struct iio_dev *indio_dev = NULL;
  653. struct ark_adc *info = NULL;
  654. struct resource *res = NULL;
  655. int ret = -ENODEV;
  656. indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct ark_adc));
  657. if (!indio_dev) {
  658. dev_err(&pdev->dev, "failed allocating iio device\n");
  659. return -ENOMEM;
  660. }
  661. info = iio_priv(indio_dev);
  662. //get data
  663. info->data = ark_adc_get_data(pdev);
  664. if (!info->data) {
  665. dev_err(&pdev->dev, "failed getting ark_adc_data\n");
  666. goto err_devm_iio_device_alloc;
  667. }
  668. //adc resource
  669. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  670. info->adc_base= devm_ioremap_resource(&pdev->dev, res);
  671. if (IS_ERR(info->adc_base)) {
  672. ret = PTR_ERR(info->adc_base);
  673. goto err_resource;
  674. }
  675. //sys resource
  676. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  677. if (IS_ERR(res)) {
  678. ret = PTR_ERR(res);
  679. goto err_resource;
  680. }
  681. //info->sys_base = devm_ioremap_resource(&pdev->dev, res);
  682. info->sys_base = ioremap(res->start, resource_size(res));
  683. if (IS_ERR(info->sys_base)) {
  684. ret = PTR_ERR(info->sys_base);
  685. goto err_resource;
  686. }
  687. //irq
  688. info->irq = platform_get_irq(pdev, 0);
  689. if (info->irq < 0) {
  690. dev_err(&pdev->dev, "no irq resource\n");
  691. ret = info->irq;
  692. goto err_irq;
  693. }
  694. //ret = request_irq(info->irq, ark_adc_intr_handler, 0, dev_name(&pdev->dev), info);
  695. ret = devm_request_irq(
  696. &pdev->dev,
  697. info->irq,
  698. ark_adc_intr_handler,
  699. 0,//IRQF_SHARED|IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, //SA_SHIRQ,
  700. dev_name(&pdev->dev),//"adc",
  701. info
  702. );
  703. if (ret < 0) {
  704. dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", info->irq);
  705. goto err_irq;
  706. }
  707. info->dev = &pdev->dev;
  708. info->value1[0] = info->value1[1] = info->value1[2] = info->value1[3] =ARK_BATTERY_VOLTAGE_REF;
  709. platform_set_drvdata(pdev, indio_dev);
  710. indio_dev->name = dev_name(&pdev->dev);
  711. indio_dev->dev.parent = &pdev->dev;
  712. indio_dev->dev.of_node = pdev->dev.of_node;
  713. indio_dev->info = &ark_adc_iio_info;
  714. indio_dev->modes = INDIO_DIRECT_MODE;
  715. indio_dev->channels = ark_adc_iio_channels;
  716. indio_dev->num_channels = info->data->num_channels;
  717. ret = iio_device_register(indio_dev);
  718. if(ret)
  719. goto err_iio_device_register;
  720. //ret = of_platform_populate(np, ark_adc_match, NULL, &indio_dev->dev);
  721. //if (ret < 0) {
  722. // dev_err(&pdev->dev, "failed adding child nodes\n");
  723. // goto err_of_populate;
  724. //}
  725. init_completion(&info->completion);
  726. if (info->data->init_hw)
  727. info->data->init_hw(info);
  728. printk("ark_adc_probe success\n");
  729. return 0;
  730. //err_iio:
  731. iio_device_unregister(indio_dev);
  732. err_iio_device_register:
  733. //free_irq(info->irq, info);
  734. //devm_free_irq(&pdev->dev, info->irq, info);
  735. err_irq:
  736. //if(info->sys_base)
  737. // devm_iounmap(&pdev->dev, info->sys_base);
  738. err_resource:
  739. //if(info->adc_base)
  740. // devm_iounmap(&pdev->dev, info->adc_base);
  741. err_devm_iio_device_alloc:
  742. //if(indio_dev)
  743. // devm_iio_device_free(&pdev->dev, indio_dev);
  744. return ret;
  745. }
  746. static int ark_adc_remove(struct platform_device *pdev)
  747. {
  748. struct iio_dev *indio_dev = platform_get_drvdata(pdev);
  749. struct ark_adc *info = iio_priv(indio_dev);
  750. if(!info)
  751. return -ENODEV;
  752. //device_for_each_child(&indio_dev->dev, NULL, ark_adc_remove_devices);
  753. iio_device_unregister(indio_dev);
  754. //free_irq(info->irq, info);
  755. //devm_free_irq(&pdev->dev, info->irq, info);
  756. //if(info->sys_base)
  757. // devm_iounmap(&pdev->dev, info->sys_base);
  758. //if(info->adc_base)
  759. // devm_iounmap(&pdev->dev, info->adc_base);
  760. //if(indio_dev)
  761. // devm_iio_device_free(&pdev->dev, indio_dev);
  762. return 0;
  763. }
  764. static struct platform_driver ark_adc_driver = {
  765. .probe = ark_adc_probe,
  766. .remove = ark_adc_remove,
  767. .driver = {
  768. .name = "ark-adc",
  769. .owner = THIS_MODULE,
  770. .of_match_table = ark_adc_match,
  771. },
  772. };
  773. module_platform_driver(ark_adc_driver);
  774. MODULE_LICENSE("GPL v2");
  775. MODULE_DESCRIPTION("Arkmicro ADC Driver");