350 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*3 * LCD Lowlevel Control Abstraction4 *5 * Copyright (C) 2003,2004 Hewlett-Packard Company6 *7 */8 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt10 11#include <linux/module.h>12#include <linux/init.h>13#include <linux/device.h>14#include <linux/lcd.h>15#include <linux/notifier.h>16#include <linux/ctype.h>17#include <linux/err.h>18#include <linux/fb.h>19#include <linux/slab.h>20 21#if defined(CONFIG_FB) || (defined(CONFIG_FB_MODULE) && \22 defined(CONFIG_LCD_CLASS_DEVICE_MODULE))23/* This callback gets called when something important happens inside a24 * framebuffer driver. We're looking if that important event is blanking,25 * and if it is, we're switching lcd power as well ...26 */27static int fb_notifier_callback(struct notifier_block *self,28 unsigned long event, void *data)29{30 struct lcd_device *ld;31 struct fb_event *evdata = data;32 33 ld = container_of(self, struct lcd_device, fb_notif);34 if (!ld->ops)35 return 0;36 37 mutex_lock(&ld->ops_lock);38 if (!ld->ops->check_fb || ld->ops->check_fb(ld, evdata->info)) {39 if (event == FB_EVENT_BLANK) {40 if (ld->ops->set_power)41 ld->ops->set_power(ld, *(int *)evdata->data);42 } else {43 if (ld->ops->set_mode)44 ld->ops->set_mode(ld, evdata->data);45 }46 }47 mutex_unlock(&ld->ops_lock);48 return 0;49}50 51static int lcd_register_fb(struct lcd_device *ld)52{53 memset(&ld->fb_notif, 0, sizeof(ld->fb_notif));54 ld->fb_notif.notifier_call = fb_notifier_callback;55 return fb_register_client(&ld->fb_notif);56}57 58static void lcd_unregister_fb(struct lcd_device *ld)59{60 fb_unregister_client(&ld->fb_notif);61}62#else63static int lcd_register_fb(struct lcd_device *ld)64{65 return 0;66}67 68static inline void lcd_unregister_fb(struct lcd_device *ld)69{70}71#endif /* CONFIG_FB */72 73static ssize_t lcd_power_show(struct device *dev, struct device_attribute *attr,74 char *buf)75{76 int rc;77 struct lcd_device *ld = to_lcd_device(dev);78 79 mutex_lock(&ld->ops_lock);80 if (ld->ops && ld->ops->get_power)81 rc = sprintf(buf, "%d\n", ld->ops->get_power(ld));82 else83 rc = -ENXIO;84 mutex_unlock(&ld->ops_lock);85 86 return rc;87}88 89static ssize_t lcd_power_store(struct device *dev,90 struct device_attribute *attr, const char *buf, size_t count)91{92 int rc;93 struct lcd_device *ld = to_lcd_device(dev);94 unsigned long power;95 96 rc = kstrtoul(buf, 0, &power);97 if (rc)98 return rc;99 100 rc = -ENXIO;101 102 mutex_lock(&ld->ops_lock);103 if (ld->ops && ld->ops->set_power) {104 pr_debug("set power to %lu\n", power);105 ld->ops->set_power(ld, power);106 rc = count;107 }108 mutex_unlock(&ld->ops_lock);109 110 return rc;111}112static DEVICE_ATTR_RW(lcd_power);113 114static ssize_t contrast_show(struct device *dev,115 struct device_attribute *attr, char *buf)116{117 int rc = -ENXIO;118 struct lcd_device *ld = to_lcd_device(dev);119 120 mutex_lock(&ld->ops_lock);121 if (ld->ops && ld->ops->get_contrast)122 rc = sprintf(buf, "%d\n", ld->ops->get_contrast(ld));123 mutex_unlock(&ld->ops_lock);124 125 return rc;126}127 128static ssize_t contrast_store(struct device *dev,129 struct device_attribute *attr, const char *buf, size_t count)130{131 int rc;132 struct lcd_device *ld = to_lcd_device(dev);133 unsigned long contrast;134 135 rc = kstrtoul(buf, 0, &contrast);136 if (rc)137 return rc;138 139 rc = -ENXIO;140 141 mutex_lock(&ld->ops_lock);142 if (ld->ops && ld->ops->set_contrast) {143 pr_debug("set contrast to %lu\n", contrast);144 ld->ops->set_contrast(ld, contrast);145 rc = count;146 }147 mutex_unlock(&ld->ops_lock);148 149 return rc;150}151static DEVICE_ATTR_RW(contrast);152 153static ssize_t max_contrast_show(struct device *dev,154 struct device_attribute *attr, char *buf)155{156 struct lcd_device *ld = to_lcd_device(dev);157 158 return sprintf(buf, "%d\n", ld->props.max_contrast);159}160static DEVICE_ATTR_RO(max_contrast);161 162static void lcd_device_release(struct device *dev)163{164 struct lcd_device *ld = to_lcd_device(dev);165 kfree(ld);166}167 168static struct attribute *lcd_device_attrs[] = {169 &dev_attr_lcd_power.attr,170 &dev_attr_contrast.attr,171 &dev_attr_max_contrast.attr,172 NULL,173};174ATTRIBUTE_GROUPS(lcd_device);175 176static const struct class lcd_class = {177 .name = "lcd",178 .dev_groups = lcd_device_groups,179};180 181/**182 * lcd_device_register - register a new object of lcd_device class.183 * @name: the name of the new object(must be the same as the name of the184 * respective framebuffer device).185 * @parent: pointer to the parent's struct device .186 * @devdata: an optional pointer to be stored in the device. The187 * methods may retrieve it by using lcd_get_data(ld).188 * @ops: the lcd operations structure.189 *190 * Creates and registers a new lcd device. Returns either an ERR_PTR()191 * or a pointer to the newly allocated device.192 */193struct lcd_device *lcd_device_register(const char *name, struct device *parent,194 void *devdata, const struct lcd_ops *ops)195{196 struct lcd_device *new_ld;197 int rc;198 199 pr_debug("lcd_device_register: name=%s\n", name);200 201 new_ld = kzalloc(sizeof(struct lcd_device), GFP_KERNEL);202 if (!new_ld)203 return ERR_PTR(-ENOMEM);204 205 mutex_init(&new_ld->ops_lock);206 mutex_init(&new_ld->update_lock);207 208 new_ld->dev.class = &lcd_class;209 new_ld->dev.parent = parent;210 new_ld->dev.release = lcd_device_release;211 dev_set_name(&new_ld->dev, "%s", name);212 dev_set_drvdata(&new_ld->dev, devdata);213 214 new_ld->ops = ops;215 216 rc = device_register(&new_ld->dev);217 if (rc) {218 put_device(&new_ld->dev);219 return ERR_PTR(rc);220 }221 222 rc = lcd_register_fb(new_ld);223 if (rc) {224 device_unregister(&new_ld->dev);225 return ERR_PTR(rc);226 }227 228 return new_ld;229}230EXPORT_SYMBOL(lcd_device_register);231 232/**233 * lcd_device_unregister - unregisters a object of lcd_device class.234 * @ld: the lcd device object to be unregistered and freed.235 *236 * Unregisters a previously registered via lcd_device_register object.237 */238void lcd_device_unregister(struct lcd_device *ld)239{240 if (!ld)241 return;242 243 mutex_lock(&ld->ops_lock);244 ld->ops = NULL;245 mutex_unlock(&ld->ops_lock);246 lcd_unregister_fb(ld);247 248 device_unregister(&ld->dev);249}250EXPORT_SYMBOL(lcd_device_unregister);251 252static void devm_lcd_device_release(struct device *dev, void *res)253{254 struct lcd_device *lcd = *(struct lcd_device **)res;255 256 lcd_device_unregister(lcd);257}258 259static int devm_lcd_device_match(struct device *dev, void *res, void *data)260{261 struct lcd_device **r = res;262 263 return *r == data;264}265 266/**267 * devm_lcd_device_register - resource managed lcd_device_register()268 * @dev: the device to register269 * @name: the name of the device270 * @parent: a pointer to the parent device271 * @devdata: an optional pointer to be stored for private driver use272 * @ops: the lcd operations structure273 *274 * @return a struct lcd on success, or an ERR_PTR on error275 *276 * Managed lcd_device_register(). The lcd_device returned from this function277 * are automatically freed on driver detach. See lcd_device_register()278 * for more information.279 */280struct lcd_device *devm_lcd_device_register(struct device *dev,281 const char *name, struct device *parent,282 void *devdata, const struct lcd_ops *ops)283{284 struct lcd_device **ptr, *lcd;285 286 ptr = devres_alloc(devm_lcd_device_release, sizeof(*ptr), GFP_KERNEL);287 if (!ptr)288 return ERR_PTR(-ENOMEM);289 290 lcd = lcd_device_register(name, parent, devdata, ops);291 if (!IS_ERR(lcd)) {292 *ptr = lcd;293 devres_add(dev, ptr);294 } else {295 devres_free(ptr);296 }297 298 return lcd;299}300EXPORT_SYMBOL(devm_lcd_device_register);301 302/**303 * devm_lcd_device_unregister - resource managed lcd_device_unregister()304 * @dev: the device to unregister305 * @ld: the lcd device to unregister306 *307 * Deallocated a lcd allocated with devm_lcd_device_register(). Normally308 * this function will not need to be called and the resource management309 * code will ensure that the resource is freed.310 */311void devm_lcd_device_unregister(struct device *dev, struct lcd_device *ld)312{313 int rc;314 315 rc = devres_release(dev, devm_lcd_device_release,316 devm_lcd_device_match, ld);317 WARN_ON(rc);318}319EXPORT_SYMBOL(devm_lcd_device_unregister);320 321 322static void __exit lcd_class_exit(void)323{324 class_unregister(&lcd_class);325}326 327static int __init lcd_class_init(void)328{329 int ret;330 331 ret = class_register(&lcd_class);332 if (ret) {333 pr_warn("Unable to create backlight class; errno = %d\n", ret);334 return ret;335 }336 337 return 0;338}339 340/*341 * if this is compiled into the kernel, we need to ensure that the342 * class is registered before users of the class try to register lcd's343 */344postcore_initcall(lcd_class_init);345module_exit(lcd_class_exit);346 347MODULE_LICENSE("GPL");348MODULE_AUTHOR("Jamey Hicks <jamey.hicks@hp.com>, Andrew Zabolotny <zap@homelink.ru>");349MODULE_DESCRIPTION("LCD Lowlevel Control Abstraction");350