ds1620.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/seq_file.h>
  10. #include <linux/capability.h>
  11. #include <linux/init.h>
  12. #include <linux/mutex.h>
  13. #include <mach/hardware.h>
  14. #include <asm/mach-types.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/therm.h>
  17. #ifdef CONFIG_PROC_FS
  18. /* define for /proc interface */
  19. #define THERM_USE_PROC
  20. #endif
  21. /* Definitions for DS1620 chip */
  22. #define THERM_START_CONVERT 0xee
  23. #define THERM_RESET 0xaf
  24. #define THERM_READ_CONFIG 0xac
  25. #define THERM_READ_TEMP 0xaa
  26. #define THERM_READ_TL 0xa2
  27. #define THERM_READ_TH 0xa1
  28. #define THERM_WRITE_CONFIG 0x0c
  29. #define THERM_WRITE_TL 0x02
  30. #define THERM_WRITE_TH 0x01
  31. #define CFG_CPU 2
  32. #define CFG_1SHOT 1
  33. static DEFINE_MUTEX(ds1620_mutex);
  34. static const char *fan_state[] = { "off", "on", "on (hardwired)" };
  35. /*
  36. * Start of NetWinder specifics
  37. * Note! We have to hold the gpio lock with IRQs disabled over the
  38. * whole of our transaction to the Dallas chip, since there is a
  39. * chance that the WaveArtist driver could touch these bits to
  40. * enable or disable the speaker.
  41. */
  42. extern unsigned int system_rev;
  43. static inline void netwinder_ds1620_set_clk(int clk)
  44. {
  45. nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
  46. }
  47. static inline void netwinder_ds1620_set_data(int dat)
  48. {
  49. nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
  50. }
  51. static inline int netwinder_ds1620_get_data(void)
  52. {
  53. return nw_gpio_read() & GPIO_DATA;
  54. }
  55. static inline void netwinder_ds1620_set_data_dir(int dir)
  56. {
  57. nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
  58. }
  59. static inline void netwinder_ds1620_reset(void)
  60. {
  61. nw_cpld_modify(CPLD_DS_ENABLE, 0);
  62. nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
  63. }
  64. static inline void netwinder_lock(unsigned long *flags)
  65. {
  66. raw_spin_lock_irqsave(&nw_gpio_lock, *flags);
  67. }
  68. static inline void netwinder_unlock(unsigned long *flags)
  69. {
  70. raw_spin_unlock_irqrestore(&nw_gpio_lock, *flags);
  71. }
  72. static inline void netwinder_set_fan(int i)
  73. {
  74. unsigned long flags;
  75. raw_spin_lock_irqsave(&nw_gpio_lock, flags);
  76. nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
  77. raw_spin_unlock_irqrestore(&nw_gpio_lock, flags);
  78. }
  79. static inline int netwinder_get_fan(void)
  80. {
  81. if ((system_rev & 0xf000) == 0x4000)
  82. return FAN_ALWAYS_ON;
  83. return (nw_gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
  84. }
  85. /*
  86. * End of NetWinder specifics
  87. */
  88. static void ds1620_send_bits(int nr, int value)
  89. {
  90. int i;
  91. for (i = 0; i < nr; i++) {
  92. netwinder_ds1620_set_data(value & 1);
  93. netwinder_ds1620_set_clk(0);
  94. udelay(1);
  95. netwinder_ds1620_set_clk(1);
  96. udelay(1);
  97. value >>= 1;
  98. }
  99. }
  100. static unsigned int ds1620_recv_bits(int nr)
  101. {
  102. unsigned int value = 0, mask = 1;
  103. int i;
  104. netwinder_ds1620_set_data(0);
  105. for (i = 0; i < nr; i++) {
  106. netwinder_ds1620_set_clk(0);
  107. udelay(1);
  108. if (netwinder_ds1620_get_data())
  109. value |= mask;
  110. mask <<= 1;
  111. netwinder_ds1620_set_clk(1);
  112. udelay(1);
  113. }
  114. return value;
  115. }
  116. static void ds1620_out(int cmd, int bits, int value)
  117. {
  118. unsigned long flags;
  119. netwinder_lock(&flags);
  120. netwinder_ds1620_set_clk(1);
  121. netwinder_ds1620_set_data_dir(0);
  122. netwinder_ds1620_reset();
  123. udelay(1);
  124. ds1620_send_bits(8, cmd);
  125. if (bits)
  126. ds1620_send_bits(bits, value);
  127. udelay(1);
  128. netwinder_ds1620_reset();
  129. netwinder_unlock(&flags);
  130. msleep(20);
  131. }
  132. static unsigned int ds1620_in(int cmd, int bits)
  133. {
  134. unsigned long flags;
  135. unsigned int value;
  136. netwinder_lock(&flags);
  137. netwinder_ds1620_set_clk(1);
  138. netwinder_ds1620_set_data_dir(0);
  139. netwinder_ds1620_reset();
  140. udelay(1);
  141. ds1620_send_bits(8, cmd);
  142. netwinder_ds1620_set_data_dir(1);
  143. value = ds1620_recv_bits(bits);
  144. netwinder_ds1620_reset();
  145. netwinder_unlock(&flags);
  146. return value;
  147. }
  148. static int cvt_9_to_int(unsigned int val)
  149. {
  150. if (val & 0x100)
  151. val |= 0xfffffe00;
  152. return val;
  153. }
  154. static void ds1620_write_state(struct therm *therm)
  155. {
  156. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  157. ds1620_out(THERM_WRITE_TL, 9, therm->lo);
  158. ds1620_out(THERM_WRITE_TH, 9, therm->hi);
  159. ds1620_out(THERM_START_CONVERT, 0, 0);
  160. }
  161. static void ds1620_read_state(struct therm *therm)
  162. {
  163. therm->lo = cvt_9_to_int(ds1620_in(THERM_READ_TL, 9));
  164. therm->hi = cvt_9_to_int(ds1620_in(THERM_READ_TH, 9));
  165. }
  166. static int ds1620_open(struct inode *inode, struct file *file)
  167. {
  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 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. static long
  247. ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  248. {
  249. int ret;
  250. mutex_lock(&ds1620_mutex);
  251. ret = ds1620_ioctl(file, cmd, arg);
  252. mutex_unlock(&ds1620_mutex);
  253. return ret;
  254. }
  255. #ifdef THERM_USE_PROC
  256. static int ds1620_proc_therm_show(struct seq_file *m, void *v)
  257. {
  258. struct therm th;
  259. int temp;
  260. ds1620_read_state(&th);
  261. temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  262. seq_printf(m, "Thermostat: HI %i.%i, LOW %i.%i; temperature: %i.%i C, fan %s\n",
  263. th.hi >> 1, th.hi & 1 ? 5 : 0,
  264. th.lo >> 1, th.lo & 1 ? 5 : 0,
  265. temp >> 1, temp & 1 ? 5 : 0,
  266. fan_state[netwinder_get_fan()]);
  267. return 0;
  268. }
  269. static int ds1620_proc_therm_open(struct inode *inode, struct file *file)
  270. {
  271. return single_open(file, ds1620_proc_therm_show, NULL);
  272. }
  273. static const struct file_operations ds1620_proc_therm_fops = {
  274. .open = ds1620_proc_therm_open,
  275. .read = seq_read,
  276. .llseek = seq_lseek,
  277. .release = single_release,
  278. };
  279. #endif
  280. static const struct file_operations ds1620_fops = {
  281. .owner = THIS_MODULE,
  282. .open = ds1620_open,
  283. .read = ds1620_read,
  284. .unlocked_ioctl = ds1620_unlocked_ioctl,
  285. .llseek = no_llseek,
  286. };
  287. static struct miscdevice ds1620_miscdev = {
  288. TEMP_MINOR,
  289. "temp",
  290. &ds1620_fops
  291. };
  292. static int __init ds1620_init(void)
  293. {
  294. int ret;
  295. struct therm th, th_start;
  296. if (!machine_is_netwinder())
  297. return -ENODEV;
  298. ds1620_out(THERM_RESET, 0, 0);
  299. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  300. ds1620_out(THERM_START_CONVERT, 0, 0);
  301. /*
  302. * Trigger the fan to start by setting
  303. * temperature high point low. This kicks
  304. * the fan into action.
  305. */
  306. ds1620_read_state(&th);
  307. th_start.lo = 0;
  308. th_start.hi = 1;
  309. ds1620_write_state(&th_start);
  310. msleep(2000);
  311. ds1620_write_state(&th);
  312. ret = misc_register(&ds1620_miscdev);
  313. if (ret < 0)
  314. return ret;
  315. #ifdef THERM_USE_PROC
  316. if (!proc_create("therm", 0, NULL, &ds1620_proc_therm_fops))
  317. printk(KERN_ERR "therm: unable to register /proc/therm\n");
  318. #endif
  319. ds1620_read_state(&th);
  320. ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  321. printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
  322. "current %i.%i C, fan %s.\n",
  323. th.hi >> 1, th.hi & 1 ? 5 : 0,
  324. th.lo >> 1, th.lo & 1 ? 5 : 0,
  325. ret >> 1, ret & 1 ? 5 : 0,
  326. fan_state[netwinder_get_fan()]);
  327. return 0;
  328. }
  329. static void __exit ds1620_exit(void)
  330. {
  331. #ifdef THERM_USE_PROC
  332. remove_proc_entry("therm", NULL);
  333. #endif
  334. misc_deregister(&ds1620_miscdev);
  335. }
  336. module_init(ds1620_init);
  337. module_exit(ds1620_exit);
  338. MODULE_LICENSE("GPL");