display-sysfs.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. static ssize_t display_show_name(struct device *dev,
  30. struct device_attribute *attr, char *buf)
  31. {
  32. struct display_device *dsp = dev_get_drvdata(dev);
  33. return snprintf(buf, PAGE_SIZE, "%s\n", dsp->name);
  34. }
  35. static ssize_t display_show_type(struct device *dev,
  36. struct device_attribute *attr, char *buf)
  37. {
  38. struct display_device *dsp = dev_get_drvdata(dev);
  39. return snprintf(buf, PAGE_SIZE, "%s\n", dsp->type);
  40. }
  41. static ssize_t display_show_contrast(struct device *dev,
  42. struct device_attribute *attr, char *buf)
  43. {
  44. struct display_device *dsp = dev_get_drvdata(dev);
  45. ssize_t rc = -ENXIO;
  46. mutex_lock(&dsp->lock);
  47. if (likely(dsp->driver) && dsp->driver->get_contrast)
  48. rc = sprintf(buf, "%d\n", dsp->driver->get_contrast(dsp));
  49. mutex_unlock(&dsp->lock);
  50. return rc;
  51. }
  52. static ssize_t display_store_contrast(struct device *dev,
  53. struct device_attribute *attr,
  54. const char *buf, size_t count)
  55. {
  56. struct display_device *dsp = dev_get_drvdata(dev);
  57. ssize_t ret = -EINVAL, size;
  58. int contrast;
  59. char *endp;
  60. contrast = simple_strtoul(buf, &endp, 0);
  61. size = endp - buf;
  62. if (*endp && isspace(*endp))
  63. size++;
  64. if (size != count)
  65. return ret;
  66. mutex_lock(&dsp->lock);
  67. if (likely(dsp->driver && dsp->driver->set_contrast)) {
  68. pr_debug("display: set contrast to %d\n", contrast);
  69. dsp->driver->set_contrast(dsp, contrast);
  70. ret = count;
  71. }
  72. mutex_unlock(&dsp->lock);
  73. return ret;
  74. }
  75. static ssize_t display_show_max_contrast(struct device *dev,
  76. struct device_attribute *attr,
  77. char *buf)
  78. {
  79. struct display_device *dsp = dev_get_drvdata(dev);
  80. ssize_t rc = -ENXIO;
  81. mutex_lock(&dsp->lock);
  82. if (likely(dsp->driver))
  83. rc = sprintf(buf, "%d\n", dsp->driver->max_contrast);
  84. mutex_unlock(&dsp->lock);
  85. return rc;
  86. }
  87. static struct device_attribute display_attrs[] = {
  88. __ATTR(name, S_IRUGO, display_show_name, NULL),
  89. __ATTR(type, S_IRUGO, display_show_type, NULL),
  90. __ATTR(contrast, S_IRUGO | S_IWUSR, display_show_contrast, display_store_contrast),
  91. __ATTR(max_contrast, S_IRUGO, display_show_max_contrast, NULL),
  92. };
  93. static int display_suspend(struct device *dev, pm_message_t state)
  94. {
  95. struct display_device *dsp = dev_get_drvdata(dev);
  96. mutex_lock(&dsp->lock);
  97. if (likely(dsp->driver->suspend))
  98. dsp->driver->suspend(dsp, state);
  99. mutex_unlock(&dsp->lock);
  100. return 0;
  101. };
  102. static int display_resume(struct device *dev)
  103. {
  104. struct display_device *dsp = dev_get_drvdata(dev);
  105. mutex_lock(&dsp->lock);
  106. if (likely(dsp->driver->resume))
  107. dsp->driver->resume(dsp);
  108. mutex_unlock(&dsp->lock);
  109. return 0;
  110. };
  111. static struct mutex allocated_dsp_lock;
  112. static DEFINE_IDR(allocated_dsp);
  113. static struct class *display_class;
  114. struct display_device *display_device_register(struct display_driver *driver,
  115. struct device *parent, void *devdata)
  116. {
  117. struct display_device *new_dev = NULL;
  118. int ret = -EINVAL;
  119. if (unlikely(!driver))
  120. return ERR_PTR(ret);
  121. mutex_lock(&allocated_dsp_lock);
  122. ret = idr_pre_get(&allocated_dsp, GFP_KERNEL);
  123. mutex_unlock(&allocated_dsp_lock);
  124. if (!ret)
  125. return ERR_PTR(ret);
  126. new_dev = kzalloc(sizeof(struct display_device), GFP_KERNEL);
  127. if (likely(new_dev) && unlikely(driver->probe(new_dev, devdata))) {
  128. // Reserve the index for this display
  129. mutex_lock(&allocated_dsp_lock);
  130. ret = idr_get_new(&allocated_dsp, new_dev, &new_dev->idx);
  131. mutex_unlock(&allocated_dsp_lock);
  132. if (!ret) {
  133. new_dev->dev = device_create(display_class, parent, 0,
  134. "display%d", new_dev->idx);
  135. if (!IS_ERR(new_dev->dev)) {
  136. dev_set_drvdata(new_dev->dev, new_dev);
  137. new_dev->parent = parent;
  138. new_dev->driver = driver;
  139. mutex_init(&new_dev->lock);
  140. return new_dev;
  141. }
  142. mutex_lock(&allocated_dsp_lock);
  143. idr_remove(&allocated_dsp, new_dev->idx);
  144. mutex_unlock(&allocated_dsp_lock);
  145. ret = -EINVAL;
  146. }
  147. }
  148. kfree(new_dev);
  149. return ERR_PTR(ret);
  150. }
  151. EXPORT_SYMBOL(display_device_register);
  152. void display_device_unregister(struct display_device *ddev)
  153. {
  154. if (!ddev)
  155. return;
  156. // Free device
  157. mutex_lock(&ddev->lock);
  158. device_unregister(ddev->dev);
  159. mutex_unlock(&ddev->lock);
  160. // Mark device index as avaliable
  161. mutex_lock(&allocated_dsp_lock);
  162. idr_remove(&allocated_dsp, ddev->idx);
  163. mutex_unlock(&allocated_dsp_lock);
  164. kfree(ddev);
  165. }
  166. EXPORT_SYMBOL(display_device_unregister);
  167. static int __init display_class_init(void)
  168. {
  169. display_class = class_create(THIS_MODULE, "display");
  170. if (IS_ERR(display_class)) {
  171. printk(KERN_ERR "Failed to create display class\n");
  172. display_class = NULL;
  173. return -EINVAL;
  174. }
  175. display_class->dev_attrs = display_attrs;
  176. display_class->suspend = display_suspend;
  177. display_class->resume = display_resume;
  178. mutex_init(&allocated_dsp_lock);
  179. return 0;
  180. }
  181. static void __exit display_class_exit(void)
  182. {
  183. class_destroy(display_class);
  184. }
  185. module_init(display_class_init);
  186. module_exit(display_class_exit);
  187. MODULE_DESCRIPTION("Display Hardware handling");
  188. MODULE_AUTHOR("James Simmons <jsimmons@infradead.org>");
  189. MODULE_LICENSE("GPL");