ds1620.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
  3. * thermometer driver (as used in the Rebel.com NetWinder)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/miscdevice.h>
  7. #include <linux/delay.h>
  8. #include <linux/proc_fs.h>
  9. #include <linux/capability.h>
  10. #include <linux/init.h>
  11. #include <linux/smp_lock.h>
  12. #include <mach/hardware.h>
  13. #include <asm/mach-types.h>
  14. #include <asm/uaccess.h>
  15. #include <asm/therm.h>
  16. #ifdef CONFIG_PROC_FS
  17. /* define for /proc interface */
  18. #define THERM_USE_PROC
  19. #endif
  20. /* Definitions for DS1620 chip */
  21. #define THERM_START_CONVERT 0xee
  22. #define THERM_RESET 0xaf
  23. #define THERM_READ_CONFIG 0xac
  24. #define THERM_READ_TEMP 0xaa
  25. #define THERM_READ_TL 0xa2
  26. #define THERM_READ_TH 0xa1
  27. #define THERM_WRITE_CONFIG 0x0c
  28. #define THERM_WRITE_TL 0x02
  29. #define THERM_WRITE_TH 0x01
  30. #define CFG_CPU 2
  31. #define CFG_1SHOT 1
  32. static const char *fan_state[] = { "off", "on", "on (hardwired)" };
  33. /*
  34. * Start of NetWinder specifics
  35. * Note! We have to hold the gpio lock with IRQs disabled over the
  36. * whole of our transaction to the Dallas chip, since there is a
  37. * chance that the WaveArtist driver could touch these bits to
  38. * enable or disable the speaker.
  39. */
  40. extern unsigned int system_rev;
  41. static inline void netwinder_ds1620_set_clk(int clk)
  42. {
  43. nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
  44. }
  45. static inline void netwinder_ds1620_set_data(int dat)
  46. {
  47. nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
  48. }
  49. static inline int netwinder_ds1620_get_data(void)
  50. {
  51. return nw_gpio_read() & GPIO_DATA;
  52. }
  53. static inline void netwinder_ds1620_set_data_dir(int dir)
  54. {
  55. nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
  56. }
  57. static inline void netwinder_ds1620_reset(void)
  58. {
  59. nw_cpld_modify(CPLD_DS_ENABLE, 0);
  60. nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
  61. }
  62. static inline void netwinder_lock(unsigned long *flags)
  63. {
  64. spin_lock_irqsave(&nw_gpio_lock, *flags);
  65. }
  66. static inline void netwinder_unlock(unsigned long *flags)
  67. {
  68. spin_unlock_irqrestore(&nw_gpio_lock, *flags);
  69. }
  70. static inline void netwinder_set_fan(int i)
  71. {
  72. unsigned long flags;
  73. spin_lock_irqsave(&nw_gpio_lock, flags);
  74. nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
  75. spin_unlock_irqrestore(&nw_gpio_lock, flags);
  76. }
  77. static inline int netwinder_get_fan(void)
  78. {
  79. if ((system_rev & 0xf000) == 0x4000)
  80. return FAN_ALWAYS_ON;
  81. return (nw_gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
  82. }
  83. /*
  84. * End of NetWinder specifics
  85. */
  86. static void ds1620_send_bits(int nr, int value)
  87. {
  88. int i;
  89. for (i = 0; i < nr; i++) {
  90. netwinder_ds1620_set_data(value & 1);
  91. netwinder_ds1620_set_clk(0);
  92. udelay(1);
  93. netwinder_ds1620_set_clk(1);
  94. udelay(1);
  95. value >>= 1;
  96. }
  97. }
  98. static unsigned int ds1620_recv_bits(int nr)
  99. {
  100. unsigned int value = 0, mask = 1;
  101. int i;
  102. netwinder_ds1620_set_data(0);
  103. for (i = 0; i < nr; i++) {
  104. netwinder_ds1620_set_clk(0);
  105. udelay(1);
  106. if (netwinder_ds1620_get_data())
  107. value |= mask;
  108. mask <<= 1;
  109. netwinder_ds1620_set_clk(1);
  110. udelay(1);
  111. }
  112. return value;
  113. }
  114. static void ds1620_out(int cmd, int bits, int value)
  115. {
  116. unsigned long flags;
  117. netwinder_lock(&flags);
  118. netwinder_ds1620_set_clk(1);
  119. netwinder_ds1620_set_data_dir(0);
  120. netwinder_ds1620_reset();
  121. udelay(1);
  122. ds1620_send_bits(8, cmd);
  123. if (bits)
  124. ds1620_send_bits(bits, value);
  125. udelay(1);
  126. netwinder_ds1620_reset();
  127. netwinder_unlock(&flags);
  128. msleep(20);
  129. }
  130. static unsigned int ds1620_in(int cmd, int bits)
  131. {
  132. unsigned long flags;
  133. unsigned int value;
  134. netwinder_lock(&flags);
  135. netwinder_ds1620_set_clk(1);
  136. netwinder_ds1620_set_data_dir(0);
  137. netwinder_ds1620_reset();
  138. udelay(1);
  139. ds1620_send_bits(8, cmd);
  140. netwinder_ds1620_set_data_dir(1);
  141. value = ds1620_recv_bits(bits);
  142. netwinder_ds1620_reset();
  143. netwinder_unlock(&flags);
  144. return value;
  145. }
  146. static int cvt_9_to_int(unsigned int val)
  147. {
  148. if (val & 0x100)
  149. val |= 0xfffffe00;
  150. return val;
  151. }
  152. static void ds1620_write_state(struct therm *therm)
  153. {
  154. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  155. ds1620_out(THERM_WRITE_TL, 9, therm->lo);
  156. ds1620_out(THERM_WRITE_TH, 9, therm->hi);
  157. ds1620_out(THERM_START_CONVERT, 0, 0);
  158. }
  159. static void ds1620_read_state(struct therm *therm)
  160. {
  161. therm->lo = cvt_9_to_int(ds1620_in(THERM_READ_TL, 9));
  162. therm->hi = cvt_9_to_int(ds1620_in(THERM_READ_TH, 9));
  163. }
  164. static int ds1620_open(struct inode *inode, struct file *file)
  165. {
  166. cycle_kernel_lock();
  167. return nonseekable_open(inode, file);
  168. }
  169. static ssize_t
  170. ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
  171. {
  172. signed int cur_temp;
  173. signed char cur_temp_degF;
  174. cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
  175. /* convert to Fahrenheit, as per wdt.c */
  176. cur_temp_degF = (cur_temp * 9) / 5 + 32;
  177. if (copy_to_user(buf, &cur_temp_degF, 1))
  178. return -EFAULT;
  179. return 1;
  180. }
  181. static int
  182. ds1620_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  183. {
  184. struct therm therm;
  185. union {
  186. struct therm __user *therm;
  187. int __user *i;
  188. } uarg;
  189. int i;
  190. uarg.i = (int __user *)arg;
  191. switch(cmd) {
  192. case CMD_SET_THERMOSTATE:
  193. case CMD_SET_THERMOSTATE2:
  194. if (!capable(CAP_SYS_ADMIN))
  195. return -EPERM;
  196. if (cmd == CMD_SET_THERMOSTATE) {
  197. if (get_user(therm.hi, uarg.i))
  198. return -EFAULT;
  199. therm.lo = therm.hi - 3;
  200. } else {
  201. if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
  202. return -EFAULT;
  203. }
  204. therm.lo <<= 1;
  205. therm.hi <<= 1;
  206. ds1620_write_state(&therm);
  207. break;
  208. case CMD_GET_THERMOSTATE:
  209. case CMD_GET_THERMOSTATE2:
  210. ds1620_read_state(&therm);
  211. therm.lo >>= 1;
  212. therm.hi >>= 1;
  213. if (cmd == CMD_GET_THERMOSTATE) {
  214. if (put_user(therm.hi, uarg.i))
  215. return -EFAULT;
  216. } else {
  217. if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
  218. return -EFAULT;
  219. }
  220. break;
  221. case CMD_GET_TEMPERATURE:
  222. case CMD_GET_TEMPERATURE2:
  223. i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  224. if (cmd == CMD_GET_TEMPERATURE)
  225. i >>= 1;
  226. return put_user(i, uarg.i) ? -EFAULT : 0;
  227. case CMD_GET_STATUS:
  228. i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
  229. return put_user(i, uarg.i) ? -EFAULT : 0;
  230. case CMD_GET_FAN:
  231. i = netwinder_get_fan();
  232. return put_user(i, uarg.i) ? -EFAULT : 0;
  233. case CMD_SET_FAN:
  234. if (!capable(CAP_SYS_ADMIN))
  235. return -EPERM;
  236. if (get_user(i, uarg.i))
  237. return -EFAULT;
  238. netwinder_set_fan(i);
  239. break;
  240. default:
  241. return -ENOIOCTLCMD;
  242. }
  243. return 0;
  244. }
  245. #ifdef THERM_USE_PROC
  246. static int
  247. proc_therm_ds1620_read(char *buf, char **start, off_t offset,
  248. int len, int *eof, void *unused)
  249. {
  250. struct therm th;
  251. int temp;
  252. ds1620_read_state(&th);
  253. temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  254. len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
  255. "temperature: %i.%i C, fan %s\n",
  256. th.hi >> 1, th.hi & 1 ? 5 : 0,
  257. th.lo >> 1, th.lo & 1 ? 5 : 0,
  258. temp >> 1, temp & 1 ? 5 : 0,
  259. fan_state[netwinder_get_fan()]);
  260. return len;
  261. }
  262. static struct proc_dir_entry *proc_therm_ds1620;
  263. #endif
  264. static const struct file_operations ds1620_fops = {
  265. .owner = THIS_MODULE,
  266. .open = ds1620_open,
  267. .read = ds1620_read,
  268. .ioctl = ds1620_ioctl,
  269. };
  270. static struct miscdevice ds1620_miscdev = {
  271. TEMP_MINOR,
  272. "temp",
  273. &ds1620_fops
  274. };
  275. static int __init ds1620_init(void)
  276. {
  277. int ret;
  278. struct therm th, th_start;
  279. if (!machine_is_netwinder())
  280. return -ENODEV;
  281. ds1620_out(THERM_RESET, 0, 0);
  282. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  283. ds1620_out(THERM_START_CONVERT, 0, 0);
  284. /*
  285. * Trigger the fan to start by setting
  286. * temperature high point low. This kicks
  287. * the fan into action.
  288. */
  289. ds1620_read_state(&th);
  290. th_start.lo = 0;
  291. th_start.hi = 1;
  292. ds1620_write_state(&th_start);
  293. msleep(2000);
  294. ds1620_write_state(&th);
  295. ret = misc_register(&ds1620_miscdev);
  296. if (ret < 0)
  297. return ret;
  298. #ifdef THERM_USE_PROC
  299. proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
  300. if (proc_therm_ds1620)
  301. proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
  302. else
  303. printk(KERN_ERR "therm: unable to register /proc/therm\n");
  304. #endif
  305. ds1620_read_state(&th);
  306. ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  307. printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
  308. "current %i.%i C, fan %s.\n",
  309. th.hi >> 1, th.hi & 1 ? 5 : 0,
  310. th.lo >> 1, th.lo & 1 ? 5 : 0,
  311. ret >> 1, ret & 1 ? 5 : 0,
  312. fan_state[netwinder_get_fan()]);
  313. return 0;
  314. }
  315. static void __exit ds1620_exit(void)
  316. {
  317. #ifdef THERM_USE_PROC
  318. remove_proc_entry("therm", NULL);
  319. #endif
  320. misc_deregister(&ds1620_miscdev);
  321. }
  322. module_init(ds1620_init);
  323. module_exit(ds1620_exit);
  324. MODULE_LICENSE("GPL");