ds1620.c 8.3 KB

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