Codebase list ddcci-driver-linux / f47cb0c
Documentation and reformatting Christoph Grenz 8 years ago
3 changed file(s) with 126 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
5555 {
5656 int ret = 0;
5757 unsigned char outer_addr = (unsigned char)(client->addr << 1);
58 unsigned xor = outer_addr; /* initial xor value */
58 unsigned xor = outer_addr; /* initial xor value */
5959
6060 /* Consistency checks */
6161 if (len > 127)
6464 /* Special case: sender to 0x6E is always 0x51 */
6565 if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
6666 addr = DDCCI_HOST_ADDR_ODD;
67 }
68 else {
67 } else {
6968 /* When sending the odd address is used */
7069 addr = addr | 1;
7170 }
112111 /* Special case: sender to 0x6E is always 0x51 */
113112 if (addr == DDCCI_DEFAULT_DEVICE_ADDR) {
114113 addr = DDCCI_HOST_ADDR_ODD;
115 }
116 else {
114 } else {
117115 /* When sending the odd address is used */
118116 addr = addr | 1;
119117 }
339337 *
340338 * Also detects all communication quirks and sets the corresponding flags
341339 * in the ddcci_bus_drv_data structure associated with client.
340 *
341 * The identification command will fail on most DDC devices, as it is optional
342 * to support, but even the "failed" response suffices to detect quirks.
342343 */
343344 static int ddcci_identify_device(struct i2c_client* client, unsigned char addr,
344345 unsigned char *buf, unsigned char len)
361362 && i2c_check_functionality(client->adapter,
362363 I2C_FUNC_SMBUS_WRITE_BYTE)) {
363364 quirks |= DDCCI_QUIRK_WRITE_BYTEWISE;
364 dev_dbg(&client->dev, "DDC/CI bus quirk detected: writes must be done bytewise\n");
365 dev_dbg(&client->dev,
366 "DDC/CI bus quirk detected: writes must be done bytewise\n");
365367 /* Some devices need writing twice after a failed blockwise write */
366368 __ddcci_write_bytewise(client, addr, true, cmd, 2);
367369 msleep(delay);
401403 if (!(quirks & DDCCI_QUIRK_SKIP_FIRST_BYTE)) {
402404 if (buffer[0] == buffer[1]) {
403405 quirks |= DDCCI_QUIRK_SKIP_FIRST_BYTE;
404 dev_dbg(&client->dev, "DDC/CI bus quirk detected: doubled first byte on read\n");
406 dev_dbg(&client->dev,
407 "DDC/CI bus quirk detected: doubled first byte on read\n");
405408 ret--;
406409 buffer++;
407410 if (ret < 3) {
412415
413416 /* validate second byte (protocol flag) */
414417 if ((buffer[1] & 0x80) != 0x80 && !(quirks & DDCCI_QUIRK_NO_PFLAG)) {
415 dev_dbg(&client->dev, "DDC/CI bus quirk detected: device omits protocol flag on responses\n");
418 dev_dbg(&client->dev,
419 "DDC/CI bus quirk detected: device omits protocol flag on responses\n");
416420 quirks |= DDCCI_QUIRK_NO_PFLAG;
417421 }
418422
429433
430434 /* verify checksum */
431435 if (xor != 0) {
432 dev_err(&client->dev, "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n",
436 dev_err(&client->dev,
437 "invalid DDC/CI response, corrupted data - xor is 0x%02x, length 0x%02x\n",
433438 xor, payload_len);
434439 return -EBADMSG;
435440 }
445450
446451 /* Character device */
447452
453 /* Data structure for an open file handle */
448454 struct ddcci_fp_data {
449455 struct ddcci_device *dev;
450456 bool exclusive;
451457 unsigned char buffer[129];
452458 };
453459
460 /* Called when the character device is opened */
454461 static int ddcci_cdev_open(struct inode* inode, struct file* filp)
455462 {
456463 struct ddcci_device *dev = container_of(inode->i_cdev,
483490 return 0;
484491 }
485492
493 /* Called when the character device is closed */
486494 static int ddcci_cdev_close(struct inode* inode, struct file* filp)
487495 {
488496 struct ddcci_fp_data *fp_data = filp->private_data;
499507 return 0;
500508 }
501509
510 /* Called when reading from the character device */
502511 static ssize_t ddcci_cdev_read(struct file* filp, char __user *buffer,
503512 size_t length, loff_t* offset)
504513 {
538547 return ret;
539548 }
540549
550 /* Called when writing to the character device */
541551 static ssize_t ddcci_cdev_write(struct file *filp, const char __user *buffer,
542552 size_t count, loff_t *offset)
543553 {
587597 return ret;
588598 }
589599
600 /* Called when seeking the character device */
590601 static loff_t ddcci_cdev_seek(struct file *filp, loff_t offset, int anchor)
591602 {
592603 return -EINVAL;
601612 .llseek = ddcci_cdev_seek
602613 };
603614
615 /* Set up the character device for a DDC/CI device */
604616 static int ddcci_setup_char_device(struct ddcci_device *device)
605617 {
606618 int ret = -EINVAL;
900912 return kasprintf(GFP_KERNEL, "bus/ddcci/%d/e%02x%02x",
901913 device->i2c_client->adapter->nr,
902914 device->outer_addr, device->inner_addr);
903 }
904 else {
915 } else {
905916 return kasprintf(GFP_KERNEL, "bus/ddcci/%d/i%02x",
906917 device->i2c_client->adapter->nr,
907918 device->inner_addr);
908919 }
909920 }
910921
922 /* Device type for main DDC/CI devices*/
911923 static struct device_type ddcci_device_type = {
912924 .name = "ddcci-device",
913925 .uevent = ddcci_device_uevent,
916928 .devnode = ddcci_devnode
917929 };
918930
931 /* Device type for dependent DDC/CI devices*/
919932 static struct device_type ddcci_dependent_type = {
920933 .name = "ddcci-dependent-device",
921934 .uevent = ddcci_device_uevent,
10691082
10701083 #define IS_ANY_ID(x) (((x)[0] == -1) && ((x)[7] == -1))
10711084
1085 /* Check if any device id in the array matches the device and return the matching id */
10721086 static const struct ddcci_device_id *ddcci_match_id(const struct ddcci_device_id *id,
10731087 const struct ddcci_device *device)
10741088 {
11641178 depth++;
11651179 else if (depth > 0)
11661180 depth--;
1167 else {
1181 else
11681182 break;
1169 }
11701183 ptr = end+1;
11711184 }
11721185 return end;
11961209 return ++ptr;
11971210 }
11981211
1212 /* Fill fields in device by parsing the capability string */
11991213 static int ddcci_parse_capstring(struct ddcci_device *device)
12001214 {
12011215 char *ptr = device->capabilities;
12221236 return 0;
12231237 }
12241238
1239 /* Probe for a device on an inner address and create a ddcci_device for it */
12251240 static int ddcci_detect_device(struct i2c_client *client, unsigned char addr,
12261241 int dependent)
12271242 {
13361351 return ret;
13371352 }
13381353
1354 /* I2C detect function: check if a main or external dependent device exists */
13391355 static int ddcci_detect(struct i2c_client *client, struct i2c_board_info *info)
13401356 {
13411357 int ret;
13771393 return -ENODEV;
13781394 }
13791395
1380 /* check response startf with outer addr */
1396 /* check response starts with outer addr */
13811397 if (buf[0] != outer_addr) {
13821398 return -ENODEV;
13831399 }
13901406 return 0;
13911407 }
13921408
1409 /* I2C probe function */
13931410 static int ddcci_probe(struct i2c_client *client, const struct i2c_device_id *id)
13941411 {
13951412 int i, ret = -ENODEV, tmp;
14081425 i2c_set_clientdata(client, drv_data);
14091426
14101427 if (id->driver_data == 0) {
1411 // Core device, probe at 0x6E
1428 /* Core device, probe at 0x6E */
14121429 main_addr = DDCCI_DEFAULT_DEVICE_ADDR;
14131430 dev_dbg(&client->dev, "probing core device [%02x]\n",
14141431 client->addr << 1);
14201437 goto err_free;
14211438 }
14221439
1423 // Detect internal dependent devices
1440 /* Detect internal dependent devices */
14241441 dev_dbg(&client->dev, "probing internal dependent devices\n");
14251442 for (i = 0; i < autoprobe_addr_count; ++i) {
14261443 addr = (unsigned short)autoprobe_addrs[i];
14321449 }
14331450 }
14341451 }
1435 }
1436 else if (id->driver_data == 1) {
1452 } else if (id->driver_data == 1) {
1453 /* External dependent device */
14371454 main_addr = client->addr << 1;
14381455 dev_dbg(&client->dev, "probing external dependent device [%02x]\n", main_addr);
14391456 if ((ret = ddcci_detect_device(client, main_addr, -1))) {
14431460 ret = -ENODEV;
14441461 goto err_free;
14451462 }
1446 }
1447 else {
1448 dev_warn(&client->dev, "probe() called with invalid i2c device id\n");
1463 } else {
1464 dev_warn(&client->dev,
1465 "probe() called with invalid i2c device id\n");
14491466 ret = -EINVAL;
14501467 }
14511468
14561473 return ret;
14571474 }
14581475
1476 /*
1477 * Callback for bus_find_device() used in ddcci_remove()
1478 *
1479 * Find next device with matching outer address not flagged with
1480 * DDCCI_FLAG_REMOVED and flag it.
1481 */
14591482 static int ddcci_remove_helper(struct device *dev, void* p)
14601483 {
14611484 unsigned char outer_addr;
14791502 return 0;
14801503 }
14811504
1505 /* I2C driver remove callback: unregister all subdevices */
14821506 static int ddcci_remove(struct i2c_client *client)
14831507 {
14841508 struct ddcci_bus_drv_data *drv_data = i2c_get_clientdata(client);
14871511
14881512 down(&drv_data->sem);
14891513 while (1) {
1490 dev = bus_find_device(&ddcci_bus_type, NULL, &outer_addr, ddcci_remove_helper);
1514 dev = bus_find_device(&ddcci_bus_type, NULL, &outer_addr,
1515 ddcci_remove_helper);
14911516 if (!dev) break;
14921517 device_unregister(dev);
14931518 put_device(dev);
15161541 .class = I2C_CLASS_DDC,
15171542 .detect = ddcci_detect,
15181543 .address_list = I2C_ADDRS(
1519 DDCCI_DEFAULT_DEVICE_ADDR>>1/*,
1520 0x08, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
1521 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
1522 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x21, 0x22, 0x23,
1523 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x31,
1524 0x32, 0x33, 0x34, 0x35, 0x36, 0x38, 0x39, 0x3B,
1525 0x3C, 0x3D, 0x3E, 0x3F, 0x41, 0x42, 0x43, 0x44,
1526 0x45, 0x46, 0x47, 0x48, 0x49, 0x51, 0x52, 0x53,
1527 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x60, 0x61,
1528 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
1529 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71,
1530 0x72, 0x73, 0x74, 0x75, 0x76, 0x77*/
1544 DDCCI_DEFAULT_DEVICE_ADDR>>1
15311545 ),
15321546 };
15331547
00 /*
1 * DDC/CI monitor driver
1 * DDC/CI monitor backlight driver
22 *
33 * Copyright (c) 2015 Christoph Grenz
44 */
3434 unsigned char used_vcp;
3535 };
3636
37 static int ddcci_monitor_writectrl(struct ddcci_device* device, unsigned char ctrl, unsigned short value)
37 static int ddcci_monitor_writectrl(struct ddcci_device* device,
38 unsigned char ctrl, unsigned short value)
3839 {
3940 unsigned char buf[4];
4041 int ret;
4546 buf[3] = (value & 255);
4647
4748 ret = ddcci_device_write(device, true, buf, sizeof(buf));
48
49 return ret;
50 }
51
52 static int ddcci_monitor_readctrl(struct ddcci_device* device, unsigned char ctrl, unsigned short *value, unsigned short *maximum)
49
50 return ret;
51 }
52
53 static int ddcci_monitor_readctrl(struct ddcci_device* device,
54 unsigned char ctrl, unsigned short *value,
55 unsigned short *maximum)
5356 {
5457 int ret;
5558 unsigned char buf[10];
56
59
5760 buf[0] = DDCCI_COMMAND_READ;
5861 buf[1] = ctrl;
5962
6265
6366 if (ret == 0) return -ENOTSUPP;
6467
65 if (ret == 8 && buf[0] == DDCCI_REPLY_READ && buf[2] == ctrl)
66 {
68 if (ret == 8 && buf[0] == DDCCI_REPLY_READ && buf[2] == ctrl)
69 {
6770 if (value) {
6871 *value = buf[6] * 256 + buf[7];
6972 }
70
73
7174 if (maximum) {
7275 *maximum = buf[4] * 256 + buf[5];
7376 }
74
77
7578 if (buf[1] == 1) return -ENOTSUPP;
7679 if (buf[1] != 0) return -EIO;
7780 return 0;
7881 }
79
82
8083 return -EIO;
8184 }
8285
97100 if (bl->props.power != FB_BLANK_UNBLANK ||
98101 bl->props.state & BL_CORE_FBBLANK)
99102 brightness = 0;
100
101 ret = ddcci_monitor_writectrl(drv_data->device, DDCCI_MONITOR_LUMINANCE, brightness);
103
104 ret = ddcci_monitor_writectrl(drv_data->device, DDCCI_MONITOR_LUMINANCE,
105 brightness);
102106 if (ret > 0) return 0;
103107 return ret;
104108 }
105109
106 static int ddcci_backlight_get_brightness(struct backlight_device *bl) {
110 static int ddcci_backlight_get_brightness(struct backlight_device *bl)
111 {
107112 unsigned short value = 0, maxval = 0;
108113 int ret;
109114 struct ddcci_monitor_drv_data *drv_data = bl_get_data(bl);
110
111 ret = ddcci_monitor_readctrl(drv_data->device, DDCCI_MONITOR_LUMINANCE, &value, &maxval);
115
116 ret = ddcci_monitor_readctrl(drv_data->device, DDCCI_MONITOR_LUMINANCE,
117 &value, &maxval);
112118 if (ret < 0)
113119 return ret;
114
120
115121 bl->props.brightness = value;
116122 bl->props.max_brightness = maxval;
117123 ret = value;
118
124
119125 return ret;
120126 }
121127
126132 .check_fb = ddcci_backlight_check_fb,
127133 };
128134
129 static int ddcci_monitor_probe(struct ddcci_device *dev, const struct ddcci_device_id *id) {
135 static int ddcci_monitor_probe(struct ddcci_device *dev,
136 const struct ddcci_device_id *id)
137 {
130138 struct ddcci_monitor_drv_data *drv_data;
131139 struct backlight_properties props;
132140 struct backlight_device *bl = NULL;
133141 int ret = 0;
134142 unsigned short brightness = 0, max_brightness = 0;
135
143
136144 /* Initialize driver data structure */
137 drv_data = devm_kzalloc(&dev->dev, sizeof(struct ddcci_monitor_drv_data), GFP_KERNEL);
145 drv_data = devm_kzalloc(&dev->dev, sizeof(struct ddcci_monitor_drv_data),
146 GFP_KERNEL);
138147 if (!drv_data) return -ENOMEM;
139148 drv_data->device = dev;
140
149
141150 /* Try getting luminance */
142 ret = ddcci_monitor_readctrl(drv_data->device, DDCCI_MONITOR_LUMINANCE, &brightness, &max_brightness);
151 ret = ddcci_monitor_readctrl(drv_data->device, DDCCI_MONITOR_LUMINANCE,
152 &brightness, &max_brightness);
143153 if (ret < 0) {
144154 if (ret == -ENOTSUPP)
145 dev_info(&dev->dev, "monitor does not support reading luminance\n");
155 dev_info(&dev->dev,
156 "monitor does not support reading luminance\n");
146157 goto err_free;
147158 }
148159 drv_data->used_vcp = DDCCI_MONITOR_LUMINANCE;
160171 dev_err(&dev->dev, "failed to register backlight\n");
161172 return PTR_ERR(bl);
162173 }
163 dev_info(&dev->dev, "registered luminance as backlight device %s\n", dev_name(&dev->dev));
174 dev_info(&dev->dev, "registered luminance as backlight device %s\n",
175 dev_name(&dev->dev));
164176
165177 goto end;
166178 err_free:
169181 return ret;
170182 }
171183
172 static int ddcci_monitor_remove(struct ddcci_device *dev) {
184 static int ddcci_monitor_remove(struct ddcci_device *dev)
185 {
173186 dev_dbg(&dev->dev, "removing device\n");
174187 return 0;
175188 }
5454 #define DDCCI_FLAG_DEPENDENT BIT(2)
5555 #define DDCCI_FLAG_EXTERNAL BIT(3)
5656
57
5857 extern struct bus_type ddcci_bus_type;
5958
6059 struct ddcci_bus_drv_data;
6160
61 /* struct ddcci_device_id - identifies DDC/CI devices for probing */
6262 struct ddcci_device_id {
6363 char prot[9];
6464 char type[9];
6969 };
7070 #define DDCCI_ANY_ID "\xFF\xFF\xFF\xFFx\xFF\xFF\xFF\xFF"
7171
72 /**
73 * struct ddcci_device - represent an DDC/CI device
74 * @outer_addr: Outer device address (I2C address << 1).
75 * @inner_addr: Inner device address.
76 * @flags: Device flags.
77 * @capabilities: Device capability string.
78 * @capabilities_len: Length of capability string.
79 * @i2c_client: Parent I2C device.
80 * @bus_drv_data: Driver internal data structure.
81 * @dev: Driver model device node for the slave.
82 * @cdev: Character device structure
83 * @cdev_sem: RW semaphore for exclusive access on character device.
84 * @prot: Device class ("protocol", from capability string)
85 * @type: Device subclass ("type", from capability string)
86 * @model: Device model (from capability string)
87 * @vendor: Device vendor (from identification command response)
88 * @module: Device module (from identification command response)
89 * @device_number: Device serial (from identification command response)
90 */
7291 struct ddcci_device {
7392 unsigned short outer_addr;
7493 unsigned short inner_addr;
89108 };
90109 #define to_ddcci_device(d) container_of(d, struct ddcci_device, dev)
91110
111 /**
112 * struct ddcci_driver - represent an DDC/CI device driver
113 * @probe: Callback for device binding
114 * @remove: Callback for device unbinding
115 * @driver: Device driver model driver
116 * @id_table: List of DDC/CI devices supported by this driver
117 *
118 * The driver.owner field should be set to the module owner of this driver.
119 * The driver.name field should be set to the name of this driver.
120 */
92121 struct ddcci_driver {
93 /* Standard driver model interfaces */
94122 int (*probe)(struct ddcci_device *, const struct ddcci_device_id *);
95123 int (*remove)(struct ddcci_device *);
96
97124 struct device_driver driver;
98125 struct ddcci_device_id *id_table;
99126 };
110137 module_driver(__ddcci_driver, ddcci_add_driver, \
111138 ddcci_del_driver)
112139
113 int ddcci_device_write(struct ddcci_device *, bool p_flag, unsigned char *data, unsigned char length);
114 int ddcci_device_read(struct ddcci_device *, bool p_flag, unsigned char *buffer, unsigned char length);
115 int ddcci_device_writeread(struct ddcci_device *, bool p_flag, unsigned char *buffer, unsigned char length, unsigned char maxlength);
140 int ddcci_device_write(struct ddcci_device *, bool p_flag, unsigned char *data,
141 unsigned char length);
142 int ddcci_device_read(struct ddcci_device *, bool p_flag, unsigned char *buffer,
143 unsigned char length);
144 int ddcci_device_writeread(struct ddcci_device *, bool p_flag,
145 unsigned char *buffer, unsigned char length,
146 unsigned char maxlength);
116147
117148 static inline void *ddcci_get_drvdata(const struct ddcci_device *dev)
118149 {