display-sysfs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * display-sysfs.c - Display output driver sysfs interface
  3. *
  4. * Copyright (C) 2007 James Simmons <jsimmons@infradead.org>
  5. *
  6. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/module.h>
  25. #include <linux/display.h>
  26. #include <linux/ctype.h>
  27. #include <linux/idr.h>
  28. #include <linux/err.h>
  29. #include <linux/kdev_t.h>
  30. #include <linux/slab.h>
  31. static ssize_t display_show_name(struct device *dev,
  32. struct device_attribute *attr, char *buf)
  33. {
  34. struct display_device *dsp = dev_get_drvdata(dev);
  35. return snprintf(buf, PAGE_SIZE, "%s\n", dsp->name);
  36. }
  37. static ssize_t display_show_type(struct device *dev,
  38. struct device_attribute *attr, char *buf)
  39. {
  40. struct display_device *dsp = dev_get_drvdata(dev);
  41. return snprintf(buf, PAGE_SIZE, "%s\n", dsp->type);
  42. }
  43. static ssize_t display_show_contrast(struct device *dev,
  44. struct device_attribute *attr, char *buf)
  45. {
  46. struct display_device *dsp = dev_get_drvdata(dev);
  47. ssize_t rc = -ENXIO;
  48. mutex_lock(&dsp->lock);
  49. if (likely(dsp->driver) && dsp->driver->get_contrast)
  50. rc = sprintf(buf, "%d\n", dsp->driver->get_contrast(dsp));
  51. mutex_unlock(&dsp->lock);
  52. return rc;
  53. }
  54. static ssize_t display_store_contrast(struct device *dev,
  55. struct device_attribute *attr,
  56. const char *buf, size_t count)
  57. {
  58. struct display_device *dsp = dev_get_drvdata(dev);
  59. ssize_t ret = -EINVAL, size;
  60. int contrast;
  61. char *endp;
  62. contrast = simple_strtoul(buf, &endp, 0);
  63. size = endp - buf;
  64. if (isspace(*endp))
  65. size++;
  66. if (size != count)
  67. return ret;
  68. mutex_lock(&dsp->lock);
  69. if (likely(dsp->driver && dsp->driver->set_contrast)) {
  70. pr_debug("display: set contrast to %d\n", contrast);
  71. dsp->driver->set_contrast(dsp, contrast);
  72. ret = count;
  73. }
  74. mutex_unlock(&dsp->lock);
  75. return ret;
  76. }
  77. static ssize_t display_show_max_contrast(struct device *dev,
  78. struct device_attribute *attr,
  79. char *buf)
  80. {
  81. struct display_device *dsp = dev_get_drvdata(dev);
  82. ssize_t rc = -ENXIO;
  83. mutex_lock(&dsp->lock);
  84. if (likely(dsp->driver))
  85. rc = sprintf(buf, "%d\n", dsp->driver->max_contrast);
  86. mutex_unlock(&dsp->lock);
  87. return rc;
  88. }
  89. static struct device_attribute display_attrs[] = {
  90. __ATTR(name, S_IRUGO, display_show_name, NULL),
  91. __ATTR(type, S_IRUGO, display_show_type, NULL),
  92. __ATTR(contrast, S_IRUGO | S_IWUSR, display_show_contrast, display_store_contrast),
  93. __ATTR(max_contrast, S_IRUGO, display_show_max_contrast, NULL),
  94. };
  95. static int display_suspend(struct device *dev, pm_message_t state)
  96. {
  97. struct display_device *dsp = dev_get_drvdata(dev);
  98. mutex_lock(&dsp->lock);
  99. if (likely(dsp->driver->suspend))
  100. dsp->driver->suspend(dsp, state);
  101. mutex_unlock(&dsp->lock);
  102. return 0;
  103. };
  104. static int display_resume(struct device *dev)
  105. {
  106. struct display_device *dsp = dev_get_drvdata(dev);
  107. mutex_lock(&dsp->lock);
  108. if (likely(dsp->driver->resume))
  109. dsp->driver->resume(dsp);
  110. mutex_unlock(&dsp->lock);
  111. return 0;
  112. };
  113. static struct mutex allocated_dsp_lock;
  114. static DEFINE_IDR(allocated_dsp);
  115. static struct class *display_class;
  116. struct display_device *display_device_register(struct display_driver *driver,
  117. struct device *parent, void *devdata)
  118. {
  119. struct display_device *new_dev = NULL;
  120. int ret = -EINVAL;
  121. if (unlikely(!driver))
  122. return ERR_PTR(ret);
  123. mutex_lock(&allocated_dsp_lock);
  124. ret = idr_pre_get(&allocated_dsp, GFP_KERNEL);
  125. mutex_unlock(&allocated_dsp_lock);
  126. if (!ret)
  127. return ERR_PTR(ret);
  128. new_dev = kzalloc(sizeof(struct display_device), GFP_KERNEL);
  129. if (likely(new_dev) && unlikely(driver->probe(new_dev, devdata))) {
  130. // Reserve the index for this display
  131. mutex_lock(&allocated_dsp_lock);
  132. ret = idr_get_new(&allocated_dsp, new_dev, &new_dev->idx);
  133. mutex_unlock(&allocated_dsp_lock);
  134. if (!ret) {
  135. new_dev->dev = device_create(display_class, parent,
  136. MKDEV(0, 0), new_dev,
  137. "display%d", new_dev->idx);
  138. if (!IS_ERR(new_dev->dev)) {
  139. new_dev->parent = parent;
  140. new_dev->driver = driver;
  141. mutex_init(&new_dev->lock);
  142. return new_dev;
  143. }
  144. mutex_lock(&allocated_dsp_lock);
  145. idr_remove(&allocated_dsp, new_dev->idx);
  146. mutex_unlock(&allocated_dsp_lock);
  147. ret = -EINVAL;
  148. }
  149. }
  150. kfree(new_dev);
  151. return ERR_PTR(ret);
  152. }
  153. EXPORT_SYMBOL(display_device_register);
  154. void display_device_unregister(struct display_device *ddev)
  155. {
  156. if (!ddev)
  157. return;
  158. // Free device
  159. mutex_lock(&ddev->lock);
  160. device_unregister(ddev->dev);
  161. mutex_unlock(&ddev->lock);
  162. // Mark device index as available
  163. mutex_lock(&allocated_dsp_lock);
  164. idr_remove(&allocated_dsp, ddev->idx);
  165. mutex_unlock(&allocated_dsp_lock);
  166. kfree(ddev);
  167. }
  168. EXPORT_SYMBOL(display_device_unregister);
  169. static int __init display_class_init(void)
  170. {
  171. display_class = class_create(THIS_MODULE, "display");
  172. if (IS_ERR(display_class)) {
  173. printk(KERN_ERR "Failed to create display class\n");
  174. display_class = NULL;
  175. return -EINVAL;
  176. }
  177. display_class->dev_attrs = display_attrs;
  178. display_class->suspend = display_suspend;
  179. display_class->resume = display_resume;
  180. mutex_init(&allocated_dsp_lock);
  181. return 0;
  182. }
  183. static void __exit display_class_exit(void)
  184. {
  185. class_destroy(display_class);
  186. }
  187. module_init(display_class_init);
  188. module_exit(display_class_exit);
  189. MODULE_DESCRIPTION("Display Hardware handling");
  190. MODULE_AUTHOR("James Simmons <jsimmons@infradead.org>");
  191. MODULE_LICENSE("GPL");