PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/ds1620.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 431 lines | 312 code | 97 blank | 22 comment | 26 complexity | 9b3d8ca321cb356fa6c86b6f751e4c7b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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/mutex.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 DEFINE_MUTEX(ds1620_mutex);
  33. static const char *fan_state[] = { "off", "on", "on (hardwired)" };
  34. /*
  35. * Start of NetWinder specifics
  36. * Note! We have to hold the gpio lock with IRQs disabled over the
  37. * whole of our transaction to the Dallas chip, since there is a
  38. * chance that the WaveArtist driver could touch these bits to
  39. * enable or disable the speaker.
  40. */
  41. extern unsigned int system_rev;
  42. static inline void netwinder_ds1620_set_clk(int clk)
  43. {
  44. nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
  45. }
  46. static inline void netwinder_ds1620_set_data(int dat)
  47. {
  48. nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
  49. }
  50. static inline int netwinder_ds1620_get_data(void)
  51. {
  52. return nw_gpio_read() & GPIO_DATA;
  53. }
  54. static inline void netwinder_ds1620_set_data_dir(int dir)
  55. {
  56. nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
  57. }
  58. static inline void netwinder_ds1620_reset(void)
  59. {
  60. nw_cpld_modify(CPLD_DS_ENABLE, 0);
  61. nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
  62. }
  63. static inline void netwinder_lock(unsigned long *flags)
  64. {
  65. spin_lock_irqsave(&nw_gpio_lock, *flags);
  66. }
  67. static inline void netwinder_unlock(unsigned long *flags)
  68. {
  69. spin_unlock_irqrestore(&nw_gpio_lock, *flags);
  70. }
  71. static inline void netwinder_set_fan(int i)
  72. {
  73. unsigned long flags;
  74. spin_lock_irqsave(&nw_gpio_lock, flags);
  75. nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
  76. spin_unlock_irqrestore(&nw_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 (nw_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. 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 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. static long
  246. ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  247. {
  248. int ret;
  249. mutex_lock(&ds1620_mutex);
  250. ret = ds1620_ioctl(file, cmd, arg);
  251. mutex_unlock(&ds1620_mutex);
  252. return ret;
  253. }
  254. #ifdef THERM_USE_PROC
  255. static int
  256. proc_therm_ds1620_read(char *buf, char **start, off_t offset,
  257. int len, int *eof, void *unused)
  258. {
  259. struct therm th;
  260. int temp;
  261. ds1620_read_state(&th);
  262. temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  263. len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
  264. "temperature: %i.%i C, fan %s\n",
  265. th.hi >> 1, th.hi & 1 ? 5 : 0,
  266. th.lo >> 1, th.lo & 1 ? 5 : 0,
  267. temp >> 1, temp & 1 ? 5 : 0,
  268. fan_state[netwinder_get_fan()]);
  269. return len;
  270. }
  271. static struct proc_dir_entry *proc_therm_ds1620;
  272. #endif
  273. static const struct file_operations ds1620_fops = {
  274. .owner = THIS_MODULE,
  275. .open = ds1620_open,
  276. .read = ds1620_read,
  277. .unlocked_ioctl = ds1620_unlocked_ioctl,
  278. .llseek = no_llseek,
  279. };
  280. static struct miscdevice ds1620_miscdev = {
  281. TEMP_MINOR,
  282. "temp",
  283. &ds1620_fops
  284. };
  285. static int __init ds1620_init(void)
  286. {
  287. int ret;
  288. struct therm th, th_start;
  289. if (!machine_is_netwinder())
  290. return -ENODEV;
  291. ds1620_out(THERM_RESET, 0, 0);
  292. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  293. ds1620_out(THERM_START_CONVERT, 0, 0);
  294. /*
  295. * Trigger the fan to start by setting
  296. * temperature high point low. This kicks
  297. * the fan into action.
  298. */
  299. ds1620_read_state(&th);
  300. th_start.lo = 0;
  301. th_start.hi = 1;
  302. ds1620_write_state(&th_start);
  303. msleep(2000);
  304. ds1620_write_state(&th);
  305. ret = misc_register(&ds1620_miscdev);
  306. if (ret < 0)
  307. return ret;
  308. #ifdef THERM_USE_PROC
  309. proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
  310. if (proc_therm_ds1620)
  311. proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
  312. else
  313. printk(KERN_ERR "therm: unable to register /proc/therm\n");
  314. #endif
  315. ds1620_read_state(&th);
  316. ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  317. printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
  318. "current %i.%i C, fan %s.\n",
  319. th.hi >> 1, th.hi & 1 ? 5 : 0,
  320. th.lo >> 1, th.lo & 1 ? 5 : 0,
  321. ret >> 1, ret & 1 ? 5 : 0,
  322. fan_state[netwinder_get_fan()]);
  323. return 0;
  324. }
  325. static void __exit ds1620_exit(void)
  326. {
  327. #ifdef THERM_USE_PROC
  328. remove_proc_entry("therm", NULL);
  329. #endif
  330. misc_deregister(&ds1620_miscdev);
  331. }
  332. module_init(ds1620_init);
  333. module_exit(ds1620_exit);
  334. MODULE_LICENSE("GPL");