| Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 2 | /* | 
 | 3 |  * Copyright (C) 2012 Bachmann electronic GmbH | 
 | 4 |  *	Christian Gmeiner <christian.gmeiner@gmail.com> | 
 | 5 |  * | 
 | 6 |  * Backlight driver for ot200 visualisation device from | 
 | 7 |  * Bachmann electronic GmbH. | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 8 |  */ | 
 | 9 |  | 
 | 10 | #include <linux/module.h> | 
 | 11 | #include <linux/fb.h> | 
 | 12 | #include <linux/backlight.h> | 
 | 13 | #include <linux/gpio.h> | 
| Greg Kroah-Hartman | 16559ae | 2013-02-04 15:35:26 -0800 | [diff] [blame] | 14 | #include <linux/platform_device.h> | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 15 | #include <linux/cs5535.h> | 
 | 16 |  | 
 | 17 | static struct cs5535_mfgpt_timer *pwm_timer; | 
 | 18 |  | 
 | 19 | /* this array defines the mapping of brightness in % to pwm frequency */ | 
 | 20 | static const u8 dim_table[101] = {0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, | 
 | 21 | 				  2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, | 
 | 22 | 				  4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, | 
 | 23 | 				  10, 10, 11, 11, 12, 12, 13, 14, 15, 15, 16, | 
 | 24 | 				  17, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, | 
 | 25 | 				  30, 31, 33, 35, 37, 39, 41, 43, 45, 47, 50, | 
 | 26 | 				  53, 55, 58, 61, 65, 68, 72, 75, 79, 84, 88, | 
 | 27 | 				  93, 97, 103, 108, 114, 120, 126, 133, 140, | 
 | 28 | 				  147, 155, 163}; | 
 | 29 |  | 
 | 30 | struct ot200_backlight_data { | 
 | 31 | 	int current_brightness; | 
 | 32 | }; | 
 | 33 |  | 
 | 34 | #define GPIO_DIMM	27 | 
 | 35 | #define SCALE		1 | 
 | 36 | #define CMP1MODE	0x2	/* compare on GE; output high on compare | 
 | 37 | 				 * greater than or equal */ | 
 | 38 | #define PWM_SETUP	(SCALE | CMP1MODE << 6 | MFGPT_SETUP_CNTEN) | 
 | 39 | #define MAX_COMP2	163 | 
 | 40 |  | 
 | 41 | static int ot200_backlight_update_status(struct backlight_device *bl) | 
 | 42 | { | 
 | 43 | 	struct ot200_backlight_data *data = bl_get_data(bl); | 
 | 44 | 	int brightness = bl->props.brightness; | 
 | 45 |  | 
 | 46 | 	if (bl->props.state & BL_CORE_FBBLANK) | 
 | 47 | 		brightness = 0; | 
 | 48 |  | 
 | 49 | 	/* enable or disable PWM timer */ | 
 | 50 | 	if (brightness == 0) | 
 | 51 | 		cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, 0); | 
 | 52 | 	else if (data->current_brightness == 0) { | 
 | 53 | 		cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0); | 
 | 54 | 		cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, | 
 | 55 | 			MFGPT_SETUP_CNTEN); | 
 | 56 | 	} | 
 | 57 |  | 
 | 58 | 	/* apply new brightness value */ | 
 | 59 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, | 
 | 60 | 		MAX_COMP2 - dim_table[brightness]); | 
 | 61 | 	data->current_brightness = brightness; | 
 | 62 |  | 
 | 63 | 	return 0; | 
 | 64 | } | 
 | 65 |  | 
 | 66 | static int ot200_backlight_get_brightness(struct backlight_device *bl) | 
 | 67 | { | 
 | 68 | 	struct ot200_backlight_data *data = bl_get_data(bl); | 
 | 69 | 	return data->current_brightness; | 
 | 70 | } | 
 | 71 |  | 
 | 72 | static const struct backlight_ops ot200_backlight_ops = { | 
 | 73 | 	.update_status	= ot200_backlight_update_status, | 
 | 74 | 	.get_brightness	= ot200_backlight_get_brightness, | 
 | 75 | }; | 
 | 76 |  | 
 | 77 | static int ot200_backlight_probe(struct platform_device *pdev) | 
 | 78 | { | 
 | 79 | 	struct backlight_device *bl; | 
 | 80 | 	struct ot200_backlight_data *data; | 
 | 81 | 	struct backlight_properties props; | 
 | 82 | 	int retval = 0; | 
 | 83 |  | 
 | 84 | 	/* request gpio */ | 
| Jingoo Han | f673934 | 2012-07-30 14:40:37 -0700 | [diff] [blame] | 85 | 	if (devm_gpio_request(&pdev->dev, GPIO_DIMM, | 
 | 86 | 				"ot200 backlight dimmer") < 0) { | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 87 | 		dev_err(&pdev->dev, "failed to request GPIO %d\n", GPIO_DIMM); | 
 | 88 | 		return -ENODEV; | 
 | 89 | 	} | 
 | 90 |  | 
 | 91 | 	/* request timer */ | 
 | 92 | 	pwm_timer = cs5535_mfgpt_alloc_timer(7, MFGPT_DOMAIN_ANY); | 
 | 93 | 	if (!pwm_timer) { | 
 | 94 | 		dev_err(&pdev->dev, "MFGPT 7 not available\n"); | 
| Jingoo Han | f673934 | 2012-07-30 14:40:37 -0700 | [diff] [blame] | 95 | 		return -ENODEV; | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 96 | 	} | 
 | 97 |  | 
| Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 98 | 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 99 | 	if (!data) { | 
 | 100 | 		retval = -ENOMEM; | 
| Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 101 | 		goto error_devm_kzalloc; | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 102 | 	} | 
 | 103 |  | 
 | 104 | 	/* setup gpio */ | 
 | 105 | 	cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_ENABLE); | 
 | 106 | 	cs5535_gpio_set(GPIO_DIMM, GPIO_OUTPUT_AUX1); | 
 | 107 |  | 
 | 108 | 	/* setup timer */ | 
 | 109 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, 0); | 
 | 110 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP2, MAX_COMP2); | 
 | 111 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, PWM_SETUP); | 
 | 112 |  | 
 | 113 | 	data->current_brightness = 100; | 
 | 114 | 	props.max_brightness = 100; | 
 | 115 | 	props.brightness = 100; | 
 | 116 | 	props.type = BACKLIGHT_RAW; | 
 | 117 |  | 
| Jingoo Han | 443956f | 2014-01-23 15:54:27 -0800 | [diff] [blame] | 118 | 	bl = devm_backlight_device_register(&pdev->dev, dev_name(&pdev->dev), | 
 | 119 | 					&pdev->dev, data, &ot200_backlight_ops, | 
 | 120 | 					&props); | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 121 | 	if (IS_ERR(bl)) { | 
 | 122 | 		dev_err(&pdev->dev, "failed to register backlight\n"); | 
 | 123 | 		retval = PTR_ERR(bl); | 
| Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 124 | 		goto error_devm_kzalloc; | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 125 | 	} | 
 | 126 |  | 
 | 127 | 	platform_set_drvdata(pdev, bl); | 
 | 128 |  | 
 | 129 | 	return 0; | 
 | 130 |  | 
| Jingoo Han | f1b60d4 | 2012-07-30 14:40:33 -0700 | [diff] [blame] | 131 | error_devm_kzalloc: | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 132 | 	cs5535_mfgpt_free_timer(pwm_timer); | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 133 | 	return retval; | 
 | 134 | } | 
 | 135 |  | 
 | 136 | static int ot200_backlight_remove(struct platform_device *pdev) | 
 | 137 | { | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 138 | 	/* on module unload set brightness to 100% */ | 
 | 139 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_COUNTER, 0); | 
 | 140 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_SETUP, MFGPT_SETUP_CNTEN); | 
 | 141 | 	cs5535_mfgpt_write(pwm_timer, MFGPT_REG_CMP1, | 
 | 142 | 		MAX_COMP2 - dim_table[100]); | 
 | 143 |  | 
 | 144 | 	cs5535_mfgpt_free_timer(pwm_timer); | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 145 |  | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 146 | 	return 0; | 
 | 147 | } | 
 | 148 |  | 
 | 149 | static struct platform_driver ot200_backlight_driver = { | 
 | 150 | 	.driver		= { | 
 | 151 | 		.name	= "ot200-backlight", | 
| Christian Gmeiner | c8df742 | 2012-03-23 15:02:02 -0700 | [diff] [blame] | 152 | 	}, | 
 | 153 | 	.probe		= ot200_backlight_probe, | 
 | 154 | 	.remove		= ot200_backlight_remove, | 
 | 155 | }; | 
 | 156 |  | 
 | 157 | module_platform_driver(ot200_backlight_driver); | 
 | 158 |  | 
 | 159 | MODULE_DESCRIPTION("backlight driver for ot200 visualisation device"); | 
 | 160 | MODULE_AUTHOR("Christian Gmeiner <christian.gmeiner@gmail.com>"); | 
 | 161 | MODULE_LICENSE("GPL"); | 
 | 162 | MODULE_ALIAS("platform:ot200-backlight"); |