saradc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. #include <linux/module.h>
  2. #include <linux/slab.h>
  3. #include <linux/device.h>
  4. #include <linux/platform_device.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/saradc.h>
  7. #include <linux/delay.h>
  8. #include "saradc_reg.h"
  9. //#define ENABLE_CALIBRATION
  10. struct saradc {
  11. // spinlock_t lock;
  12. bool busy;
  13. struct calibration *cal;
  14. int cal_num;
  15. };
  16. static struct saradc *gp_saradc;
  17. #define CHAN_XP CHAN_0
  18. #define CHAN_YP CHAN_1
  19. #define CHAN_XN CHAN_2
  20. #define CHAN_YN CHAN_3
  21. #define INTERNAL_CAL_NUM 5
  22. static u8 chan_mux[SARADC_CHAN_NUM] = {0,1,2,3,4,5,6,7};
  23. static void saradc_reset(void)
  24. {
  25. int i;
  26. //set adc clock as 1.28Mhz
  27. set_clock_divider(20);
  28. enable_clock();
  29. enable_adc();
  30. set_sample_mode(DIFF_MODE);
  31. set_tempsen(0);
  32. disable_fifo_irq();
  33. disable_continuous_sample();
  34. disable_chan0_delta();
  35. disable_chan1_delta();
  36. set_input_delay(10, INPUT_DELAY_TB_1US);
  37. set_sample_delay(10, SAMPLE_DELAY_TB_1US);
  38. set_block_delay(10, BLOCK_DELAY_TB_1US);
  39. // channels sampling mode setting
  40. for(i=0; i<SARADC_CHAN_NUM; i++) {
  41. set_sample_sw(i, IDLE_SW);
  42. set_sample_mux(i, chan_mux[i]);
  43. }
  44. // idle mode setting
  45. set_idle_sw(IDLE_SW);
  46. set_idle_mux(chan_mux[CHAN_0]);
  47. // detect mode setting
  48. set_detect_sw(DETECT_SW);
  49. set_detect_mux(chan_mux[CHAN_0]);
  50. disable_detect_sw();
  51. disable_detect_pullup();
  52. set_detect_irq_pol(0);
  53. disable_detect_irq();
  54. // set_sc_phase();
  55. enable_sample_engine();
  56. // printk("ADCREG reg0 =%x\n", get_reg(SAR_ADC_REG0));
  57. // printk("ADCREG ch list =%x\n", get_reg(SAR_ADC_CHAN_LIST));
  58. // printk("ADCREG avg =%x\n", get_reg(SAR_ADC_AVG_CNTL));
  59. // printk("ADCREG reg3=%x\n", get_reg(SAR_ADC_REG3));
  60. // printk("ADCREG ch72 sw=%x\n", get_reg(SAR_ADC_AUX_SW));
  61. // printk("ADCREG ch10 sw=%x\n", get_reg(SAR_ADC_CHAN_10_SW));
  62. // printk("ADCREG detect&idle=%x\n", get_reg(SAR_ADC_DETECT_IDLE_SW));
  63. }
  64. #ifdef ENABLE_CALIBRATION
  65. static int saradc_internal_cal(struct calibration *cal)
  66. {
  67. int i;
  68. int voltage[] = {CAL_VOLTAGE_1, CAL_VOLTAGE_2, CAL_VOLTAGE_3, CAL_VOLTAGE_4, CAL_VOLTAGE_5};
  69. int ref[] = {0, 256, 512, 768, 1023};
  70. // set_cal_mux(MUX_CAL);
  71. // enable_cal_res_array();
  72. for (i=0; i<INTERNAL_CAL_NUM; i++) {
  73. set_cal_voltage(voltage[i]);
  74. msleep(100);
  75. cal[i].ref = ref[i];
  76. cal[i].val = get_adc_sample(CHAN_7);
  77. printk(KERN_INFO "cal[%d]=%d\n", i, cal[i].val);
  78. if (cal[i].val < 0) {
  79. return -1;
  80. }
  81. }
  82. return 0;
  83. }
  84. static int saradc_get_cal_value(struct calibration *cal, int num, int val)
  85. {
  86. int ret = -1;
  87. int i;
  88. if (num < 2)
  89. return val;
  90. if (val <= cal[0].val)
  91. return cal[0].ref;
  92. if (val >= cal[num-1].val)
  93. return cal[num-1].ref;
  94. for (i=0; i<num-1; i++) {
  95. if (val < cal[i+1].val) {
  96. ret = val - cal[i].val;
  97. ret *= cal[i+1].ref - cal[i].ref;
  98. ret /= cal[i+1].val - cal[i].val;
  99. ret += cal[i].ref;
  100. break;
  101. }
  102. }
  103. return ret;
  104. }
  105. #endif
  106. static int last_value[] = {-1,-1,-1,-1,-1 ,-1,-1 ,-1};
  107. static u8 print_flag = 0; //(1<<CHAN_4)
  108. int get_adc_sample(int chan)
  109. {
  110. int count;
  111. int value;
  112. int sum;
  113. int changed;
  114. if (!gp_saradc || gp_saradc->busy)
  115. return -1;
  116. //spin_lock(&gp_saradc->lock);
  117. gp_saradc->busy = 1;
  118. set_chan_list(chan, 1);
  119. set_avg_mode(chan, NO_AVG_MODE, SAMPLE_NUM_8);
  120. set_sample_mux(chan, chan_mux[chan]);
  121. set_detect_mux(chan_mux[chan]);
  122. set_idle_mux(chan_mux[chan]); // for revb
  123. enable_sample_engine();
  124. start_sample();
  125. // Read any CBUS register to delay one clock cycle after starting the sampling engine
  126. // The bus is really fast and we may miss that it started
  127. { count = get_reg(ISA_TIMERE); }
  128. count = 0;
  129. while (delta_busy() || sample_busy() || avg_busy()) {
  130. if (++count > 10000) {
  131. printk(KERN_ERR "ADC busy error=%x.\n", READ_CBUS_REG(SAR_ADC_REG0));
  132. goto end;
  133. }
  134. }
  135. stop_sample();
  136. sum = 0;
  137. count = 0;
  138. value = get_fifo_sample();
  139. while (get_fifo_cnt()) {
  140. value = get_fifo_sample() & 0x3ff;
  141. //if ((value != 0x1fe) && (value != 0x1ff)) {
  142. sum += value & 0x3ff;
  143. count++;
  144. //}
  145. }
  146. value = (count) ? (sum / count) : (-1);
  147. end:
  148. changed = (abs(value-last_value[chan])<3) ? 0 : 1;
  149. if (changed && ((print_flag>>chan)&1)) {
  150. printk("before cal: ch%d = %d\n", chan, value);
  151. last_value[chan] = value;
  152. }
  153. #ifdef ENABLE_CALIBRATION
  154. if (gp_saradc->cal_num) {
  155. value = saradc_get_cal_value(gp_saradc->cal, gp_saradc->cal_num, value);
  156. if (changed && ((print_flag>>chan)&1))
  157. printk("after cal: ch%d = %d\n\n", chan, value);
  158. }
  159. #endif
  160. disable_sample_engine();
  161. // set_sc_phase();
  162. //spin_unlock(&gp_saradc->lock);
  163. gp_saradc->busy = 0;
  164. return value;
  165. }
  166. int saradc_ts_service(int cmd)
  167. {
  168. int value = -1;
  169. switch (cmd) {
  170. case CMD_GET_X:
  171. //set_sample_sw(CHAN_YP, X_SW);
  172. value = get_adc_sample(CHAN_YP);
  173. set_sample_sw(CHAN_XP, Y_SW); // preset for y
  174. break;
  175. case CMD_GET_Y:
  176. //set_sample_sw(CHAN_XP, Y_SW);
  177. value = get_adc_sample(CHAN_XP);
  178. break;
  179. case CMD_GET_Z1:
  180. set_sample_sw(CHAN_XP, Z1_SW);
  181. value = get_adc_sample(CHAN_XP);
  182. break;
  183. case CMD_GET_Z2:
  184. set_sample_sw(CHAN_YN, Z2_SW);
  185. value = get_adc_sample(CHAN_YN);
  186. break;
  187. case CMD_GET_PENDOWN:
  188. value = !detect_level();
  189. set_sample_sw(CHAN_YP, X_SW); // preset for x
  190. break;
  191. case CMD_INIT_PENIRQ:
  192. enable_detect_pullup();
  193. enable_detect_sw();
  194. value = 0;
  195. printk(KERN_INFO "init penirq ok\n");
  196. break;
  197. case CMD_SET_PENIRQ:
  198. enable_detect_pullup();
  199. enable_detect_sw();
  200. value = 0;
  201. break;
  202. case CMD_CLEAR_PENIRQ:
  203. disable_detect_pullup();
  204. disable_detect_sw();
  205. value = 0;
  206. break;
  207. default:
  208. break;
  209. }
  210. return value;
  211. }
  212. static ssize_t saradc_ch0_show(struct class *cla, struct class_attribute *attr, char *buf)
  213. {
  214. return sprintf(buf, "%d\n", get_adc_sample(0));
  215. }
  216. static ssize_t saradc_ch1_show(struct class *cla, struct class_attribute *attr, char *buf)
  217. {
  218. return sprintf(buf, "%d\n", get_adc_sample(1));
  219. }
  220. static ssize_t saradc_ch2_show(struct class *cla, struct class_attribute *attr, char *buf)
  221. {
  222. return sprintf(buf, "%d\n", get_adc_sample(2));
  223. }
  224. static ssize_t saradc_ch3_show(struct class *cla, struct class_attribute *attr, char *buf)
  225. {
  226. return sprintf(buf, "%d\n", get_adc_sample(3));
  227. }
  228. static ssize_t saradc_ch4_show(struct class *cla, struct class_attribute *attr, char *buf)
  229. {
  230. return sprintf(buf, "%d\n", get_adc_sample(4));
  231. }
  232. static ssize_t saradc_ch5_show(struct class *cla, struct class_attribute *attr, char *buf)
  233. {
  234. return sprintf(buf, "%d\n", get_adc_sample(5));
  235. }
  236. static ssize_t saradc_ch6_show(struct class *cla, struct class_attribute *attr, char *buf)
  237. {
  238. return sprintf(buf, "%d\n", get_adc_sample(6));
  239. }
  240. static ssize_t saradc_ch7_show(struct class *cla, struct class_attribute *attr, char *buf)
  241. {
  242. return sprintf(buf, "%d\n", get_adc_sample(7));
  243. }
  244. static ssize_t saradc_print_flag_store(struct class *cla, struct class_attribute *attr, char *buf, ssize_t count)
  245. {
  246. sscanf(buf, "%x", (int*)&print_flag);
  247. printk("print_flag=%d\n", print_flag);
  248. return count;
  249. }
  250. static ssize_t saradc_temperature_store(struct class *cla, struct class_attribute *attr, char *buf, ssize_t count)
  251. {
  252. u8 tempsen;
  253. sscanf(buf, "%x", (int*)&tempsen);
  254. if (tempsen) {
  255. temp_sens_sel(1);
  256. set_tempsen(2);
  257. printk("enter temperature mode, please get the value from chan6\n");
  258. }
  259. else {
  260. temp_sens_sel(0);
  261. set_tempsen(0);
  262. printk("exit temperature mode\n");
  263. }
  264. return count;
  265. }
  266. static struct class_attribute saradc_class_attrs[] = {
  267. __ATTR_RO(saradc_ch0),
  268. __ATTR_RO(saradc_ch1),
  269. __ATTR_RO(saradc_ch2),
  270. __ATTR_RO(saradc_ch3),
  271. __ATTR_RO(saradc_ch4),
  272. __ATTR_RO(saradc_ch5),
  273. __ATTR_RO(saradc_ch6),
  274. __ATTR_RO(saradc_ch7),
  275. __ATTR(saradc_print_flag, S_IRUGO | S_IWUSR, 0, saradc_print_flag_store),
  276. __ATTR(saradc_temperature, S_IRUGO | S_IWUSR, 0, saradc_temperature_store),
  277. __ATTR_NULL
  278. };
  279. static struct class saradc_class = {
  280. .name = "saradc",
  281. .class_attrs = saradc_class_attrs,
  282. };
  283. static int __devinit saradc_probe(struct platform_device *pdev)
  284. {
  285. int err;
  286. struct saradc *saradc;
  287. struct saradc_platform_data *pdata = pdev->dev.platform_data;
  288. saradc = kzalloc(sizeof(struct saradc), GFP_KERNEL);
  289. if (!saradc) {
  290. err = -ENOMEM;
  291. goto err_free_mem;
  292. }
  293. saradc_reset();
  294. gp_saradc = saradc;
  295. gp_saradc->busy = 0;
  296. saradc->cal = 0;
  297. saradc->cal_num = 0;
  298. #ifdef ENABLE_CALIBRATION
  299. if (pdata && pdata->cal) {
  300. saradc->cal = pdata->cal;
  301. saradc->cal_num = pdata->cal_num;
  302. printk(KERN_INFO "saradc use signed calibration data\n");
  303. }
  304. else {
  305. printk(KERN_INFO "saradc use internal calibration\n");
  306. saradc->cal = kzalloc(sizeof(struct calibration) *
  307. INTERNAL_CAL_NUM, GFP_KERNEL);
  308. if (saradc->cal) {
  309. if (saradc_internal_cal(saradc->cal) < 0) {
  310. kfree(saradc->cal);
  311. saradc->cal = 0;
  312. printk(KERN_INFO "saradc calibration fail\n");
  313. }
  314. else {
  315. saradc->cal_num = INTERNAL_CAL_NUM;
  316. printk(KERN_INFO "saradc calibration ok\n");
  317. }
  318. }
  319. }
  320. #endif
  321. set_cal_voltage(7);
  322. //spin_lock_init(&saradc->lock);
  323. gp_saradc->busy = 0;
  324. return 0;
  325. err_free_mem:
  326. kfree(saradc);
  327. printk(KERN_INFO "saradc probe error\n");
  328. return err;
  329. }
  330. static int __devexit saradc_remove(struct platform_device *pdev)
  331. {
  332. struct saradc *saradc = platform_get_drvdata(pdev);
  333. disable_adc();
  334. disable_sample_engine();
  335. gp_saradc = 0;
  336. kfree(saradc);
  337. return 0;
  338. }
  339. static struct platform_driver saradc_driver = {
  340. .probe = saradc_probe,
  341. .remove = saradc_remove,
  342. .suspend = NULL,
  343. .resume = NULL,
  344. .driver = {
  345. .name = "saradc",
  346. },
  347. };
  348. static int __devinit saradc_init(void)
  349. {
  350. printk(KERN_INFO "SARADC Driver init.\n");
  351. class_register(&saradc_class);
  352. return platform_driver_register(&saradc_driver);
  353. }
  354. static void __exit saradc_exit(void)
  355. {
  356. printk(KERN_INFO "SARADC Driver exit.\n");
  357. platform_driver_unregister(&saradc_driver);
  358. class_unregister(&saradc_class);
  359. }
  360. module_init(saradc_init);
  361. module_exit(saradc_exit);
  362. MODULE_AUTHOR("aml");
  363. MODULE_DESCRIPTION("SARADC Driver");
  364. MODULE_LICENSE("GPL");