com12v.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <linux/module.h>
  2. #include <linux/fs.h>
  3. #include <linux/cdev.h>
  4. #include <linux/device.h>
  5. #include <linux/uaccess.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/io.h>
  9. #define DEVICE_NAME "com12v"
  10. #define CLASS_NAME "com12v_class"
  11. #define MAJOR_NUM 56
  12. #define MINOR_NUM 20
  13. #define DEVICE_COUNT 1
  14. #define NUM_COMPORTS 6
  15. #define A_LONG_TIME 100 // 10.0 seconds
  16. #define DEFAULT_DELAY 20 // 2.0 second
  17. typedef enum
  18. {
  19. PPS_OFF = 0,
  20. PPS_AWAITING_ON,
  21. PPS_ON,
  22. NUM_POWER_PORT_STATES
  23. } ENUM_POWER_PORT_STATES;
  24. static struct class *com12v_class = NULL;
  25. static struct device *com12v_device = NULL;
  26. static struct cdev com12v_cdev;
  27. static dev_t dev_num;
  28. static unsigned int muiPowerOnDelayCount = DEFAULT_DELAY;
  29. static unsigned int mauiPowerPortState[NUM_COMPORTS];
  30. static struct delayed_work delay_work1;
  31. static unsigned int com12v_ctl_phy_addr[NUM_COMPORTS] = {0xFD6E0750, 0xFD6E0760, 0xFD6E0770, 0xFD6E0780, 0xFD6D0920, 0xFD6D0930};
  32. static void __iomem *com12v_ctl_vir_addr[NUM_COMPORTS];
  33. static void com12v_delay_work_func(struct work_struct *work)
  34. {
  35. static unsigned int uiTimeSinceLastPowerOn = A_LONG_TIME;
  36. int i;
  37. unsigned int value = 0;
  38. // Keep incrementing the uiTimeSinceLastPowerOn until it is large enough
  39. // that we can safely assume a long time has passed since the last port
  40. // transitioned on.
  41. if (uiTimeSinceLastPowerOn < A_LONG_TIME)
  42. {
  43. ++uiTimeSinceLastPowerOn;
  44. }
  45. // If a port is waiting to power on and enough time has passed since the
  46. // last port powered on then go ahead and power on the waiting port. The
  47. // break statement insure we don't power on more than one port at a time.
  48. for (i=0; i < NUM_COMPORTS; ++i)
  49. {
  50. // printk("%d", mauiPowerPortState[i]);
  51. if ((mauiPowerPortState[i] == PPS_AWAITING_ON) && (uiTimeSinceLastPowerOn >= muiPowerOnDelayCount))
  52. {
  53. mauiPowerPortState[i] = PPS_ON;
  54. uiTimeSinceLastPowerOn = 0;
  55. break;
  56. }
  57. }
  58. // printk("\n");
  59. // Turn on/off hw power supply pins based on current states.
  60. for (i=0; i < NUM_COMPORTS; ++i)
  61. {
  62. if (mauiPowerPortState[i] == PPS_ON)
  63. {
  64. value = readl(com12v_ctl_vir_addr[i]);
  65. value &= 0xfffffffe;
  66. writel(value, com12v_ctl_vir_addr[i]);
  67. }
  68. else if (mauiPowerPortState[i] == PPS_OFF)
  69. {
  70. value = readl(com12v_ctl_vir_addr[i]);
  71. value |= 0x1;
  72. writel(value, com12v_ctl_vir_addr[i]);
  73. }
  74. }
  75. schedule_delayed_work(&delay_work1, msecs_to_jiffies(100));
  76. }
  77. static int com12v_open(struct inode *inode, struct file *file)
  78. {
  79. // pr_info("com12v: device opened\n");
  80. return 0;
  81. }
  82. static int com12v_release(struct inode *inode, struct file *file)
  83. {
  84. // pr_info("com12v: device closed\n");
  85. return 0;
  86. }
  87. static ssize_t com12v_read(struct file *file, char __user *user_buf,
  88. size_t len, loff_t *offset)
  89. {
  90. size_t read_len;
  91. return read_len;
  92. }
  93. static ssize_t com12v_write(struct file *file, const char __user *user_buf,
  94. size_t len, loff_t *offset)
  95. {
  96. int i = 0;
  97. int iPortIndex;
  98. char buff[1024];
  99. char c;
  100. size_t write_len;
  101. if (len > sizeof(buff))
  102. {
  103. write_len = sizeof(buff);
  104. }
  105. else
  106. {
  107. write_len = len;
  108. }
  109. if (copy_from_user(buff, user_buf, write_len))
  110. {
  111. return -EFAULT;
  112. }
  113. for (i = 0; i < write_len; i++)
  114. {
  115. c = buff[i];
  116. if ((c >= 'A') && (c <= 'F'))
  117. {
  118. iPortIndex = c - 'A';
  119. if (muiPowerOnDelayCount == 0)
  120. {
  121. mauiPowerPortState[iPortIndex] = PPS_ON;
  122. }
  123. else
  124. {
  125. if (mauiPowerPortState[iPortIndex] == PPS_OFF)
  126. {
  127. mauiPowerPortState[iPortIndex] = PPS_AWAITING_ON;
  128. }
  129. }
  130. }
  131. else if ((c >= 'a') && (c <= 'f'))
  132. {
  133. iPortIndex = c - 'a';
  134. mauiPowerPortState[iPortIndex] = PPS_OFF;
  135. }
  136. else if ((c >= '0') && (c <= '9'))
  137. {
  138. muiPowerOnDelayCount = (c - '0') * 10;
  139. }
  140. else
  141. {
  142. printk("com12v: invalid character %c\n", c);
  143. }
  144. }
  145. return write_len;
  146. }
  147. static struct file_operations com12v_fops = {
  148. .owner = THIS_MODULE,
  149. .open = com12v_open,
  150. .release = com12v_release,
  151. .read = com12v_read,
  152. .write = com12v_write,
  153. };
  154. int com12v_init(void)
  155. {
  156. int ret;
  157. int i = 0;
  158. for(i = 0; i < NUM_COMPORTS; i++)
  159. {
  160. com12v_ctl_vir_addr[i] = ioremap(com12v_ctl_phy_addr[i], 4);
  161. }
  162. pr_info("com12v: initializing driver\n");
  163. memset(mauiPowerPortState, 0, sizeof(mauiPowerPortState));
  164. INIT_DELAYED_WORK(&delay_work1, com12v_delay_work_func);
  165. schedule_delayed_work(&delay_work1, msecs_to_jiffies(100));
  166. dev_num = MKDEV(MAJOR_NUM, MINOR_NUM);
  167. ret = register_chrdev_region(dev_num, DEVICE_COUNT, DEVICE_NAME);
  168. if (ret < 0)
  169. {
  170. pr_err("com12v: Failed to register device number %d:%d\n",
  171. MAJOR_NUM, MINOR_NUM);
  172. return ret;
  173. }
  174. cdev_init(&com12v_cdev, &com12v_fops);
  175. com12v_cdev.owner = THIS_MODULE;
  176. ret = cdev_add(&com12v_cdev, dev_num, DEVICE_COUNT);
  177. if (ret < 0)
  178. {
  179. pr_err("com12v: Failed to add cdev\n");
  180. goto err_unregister_region;
  181. }
  182. com12v_class = class_create(THIS_MODULE, CLASS_NAME);
  183. if (IS_ERR(com12v_class))
  184. {
  185. pr_err("com12v: Failed to create class\n");
  186. ret = PTR_ERR(com12v_class);
  187. goto err_cdev_del;
  188. }
  189. com12v_device = device_create(com12v_class, NULL, dev_num,
  190. NULL, DEVICE_NAME);
  191. if (IS_ERR(com12v_device))
  192. {
  193. pr_err("com12v: Failed to create device\n");
  194. ret = PTR_ERR(com12v_device);
  195. goto err_class_destroy;
  196. }
  197. pr_info("com12v: driver initialized successfully, device node /dev/%s\n",
  198. DEVICE_NAME);
  199. return 0;
  200. err_class_destroy:
  201. class_destroy(com12v_class);
  202. err_cdev_del:
  203. cdev_del(&com12v_cdev);
  204. err_unregister_region:
  205. unregister_chrdev_region(dev_num, DEVICE_COUNT);
  206. return ret;
  207. }
  208. void com12v_exit(void)
  209. {
  210. int i = 0;
  211. cancel_delayed_work_sync(&delay_work1);
  212. device_destroy(com12v_class, dev_num);
  213. class_destroy(com12v_class);
  214. cdev_del(&com12v_cdev);
  215. unregister_chrdev_region(dev_num, DEVICE_COUNT);
  216. for(i = 0; i < NUM_COMPORTS; i++)
  217. {
  218. iounmap(com12v_ctl_vir_addr[i]);
  219. }
  220. pr_info("com12v: driver unloaded\n");
  221. }