30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
49 #define APPLESMC_DATA_PORT 0x300
51 #define APPLESMC_CMD_PORT 0x304
53 #define APPLESMC_NR_PORTS 32
55 #define APPLESMC_MAX_DATA_LENGTH 32
58 #define APPLESMC_MIN_WAIT 0x0010
59 #define APPLESMC_RETRY_WAIT 0x0100
60 #define APPLESMC_MAX_WAIT 0x20000
62 #define APPLESMC_READ_CMD 0x10
63 #define APPLESMC_WRITE_CMD 0x11
64 #define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
65 #define APPLESMC_GET_KEY_TYPE_CMD 0x13
67 #define KEY_COUNT_KEY "#KEY"
69 #define LIGHT_SENSOR_LEFT_KEY "ALV0"
70 #define LIGHT_SENSOR_RIGHT_KEY "ALV1"
71 #define BACKLIGHT_KEY "LKSB"
73 #define CLAMSHELL_KEY "MSLD"
75 #define MOTION_SENSOR_X_KEY "MO_X"
76 #define MOTION_SENSOR_Y_KEY "MO_Y"
77 #define MOTION_SENSOR_Z_KEY "MO_Z"
78 #define MOTION_SENSOR_KEY "MOCN"
80 #define FANS_COUNT "FNum"
81 #define FANS_MANUAL "FS! "
82 #define FAN_ID_FMT "F%dID"
84 #define TEMP_SENSOR_TYPE "sp78"
87 static const char *
const fan_speed_fmt[] = {
95 #define INIT_TIMEOUT_MSECS 5000
96 #define INIT_WAIT_MSECS 50
98 #define APPLESMC_POLL_INTERVAL 50
99 #define APPLESMC_INPUT_FUZZ 4
100 #define APPLESMC_INPUT_FLAT 4
102 #define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff)
103 #define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16)
130 static struct applesmc_registers {
132 unsigned int key_count;
133 unsigned int fan_count;
134 unsigned int temp_count;
135 unsigned int temp_begin;
136 unsigned int temp_end;
137 unsigned int index_count;
138 int num_light_sensors;
139 bool has_accelerometer;
140 bool has_key_backlight;
148 static const int debug;
152 static u8 backlight_state[2];
154 static struct device *hwmon_dev;
155 static struct input_polled_dev *applesmc_idev;
161 static unsigned int key_at_index;
169 static int wait_read(
void)
181 pr_warn(
"wait_read() fail: 0x%02x\n", status);
205 if (us << 1 == APPLESMC_MAX_WAIT)
212 pr_warn(
"send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
216 static int send_command(
u8 cmd)
221 static int send_argument(
const char *
key)
225 for (i = 0; i < 4; i++)
231 static int read_smc(
u8 cmd,
const char *key,
u8 *
buffer,
u8 len)
235 if (send_command(cmd) || send_argument(key)) {
236 pr_warn(
"%.4s: read arg fail\n", key);
241 pr_warn(
"%.4s: read len fail\n", key);
245 for (i = 0; i < len; i++) {
247 pr_warn(
"%.4s: read data[%d] fail\n", key, i);
256 static int write_smc(
u8 cmd,
const char *key,
const u8 *buffer,
u8 len)
260 if (send_command(cmd) || send_argument(key)) {
261 pr_warn(
"%s: write arg fail\n", key);
266 pr_warn(
"%.4s: write len fail\n", key);
270 for (i = 0; i < len; i++) {
272 pr_warn(
"%s: write data fail\n", key);
280 static int read_register_count(
unsigned int *
count)
305 if (entry->
len != len)
314 static int applesmc_write_entry(
const struct applesmc_entry *entry,
315 const u8 *buf,
u8 len)
319 if (entry->
len != len)
350 cache->
len = info[0];
352 cache->
flags = info[5];
362 static int applesmc_get_lower_bound(
unsigned int *lo,
const char *key)
364 int begin = 0,
end = smcreg.key_count;
367 while (begin !=
end) {
368 int middle = begin + (
end - begin) / 2;
369 entry = applesmc_get_entry_by_index(middle);
372 return PTR_ERR(entry);
374 if (
strcmp(entry->key, key) < 0)
384 static int applesmc_get_upper_bound(
unsigned int *hi,
const char *key)
386 int begin = 0,
end = smcreg.key_count;
389 while (begin !=
end) {
390 int middle = begin + (
end - begin) / 2;
391 entry = applesmc_get_entry_by_index(middle);
393 *hi = smcreg.key_count;
394 return PTR_ERR(entry);
396 if (
strcmp(key, entry->key) < 0)
406 static const struct applesmc_entry *applesmc_get_entry_by_key(
const char *key)
411 ret = applesmc_get_lower_bound(&begin, key);
414 ret = applesmc_get_upper_bound(&end, key);
417 if (end - begin != 1)
420 return applesmc_get_entry_by_index(begin);
423 static int applesmc_read_key(
const char *key,
u8 *buffer,
u8 len)
427 entry = applesmc_get_entry_by_key(key);
429 return PTR_ERR(entry);
431 return applesmc_read_entry(entry, buffer, len);
434 static int applesmc_write_key(
const char *key,
const u8 *buffer,
u8 len)
438 entry = applesmc_get_entry_by_key(key);
440 return PTR_ERR(entry);
442 return applesmc_write_entry(entry, buffer, len);
445 static int applesmc_has_key(
const char *key,
bool *
value)
449 entry = applesmc_get_entry_by_key(key);
450 if (IS_ERR(entry) && PTR_ERR(entry) != -
EINVAL)
451 return PTR_ERR(entry);
453 *value = !IS_ERR(entry);
460 static int applesmc_read_s16(
const char *key,
s16 *value)
465 ret = applesmc_read_key(key, buffer, 2);
469 *value = ((
s16)buffer[0] << 8) | buffer[1];
476 static void applesmc_device_init(
void)
481 if (!smcreg.has_accelerometer)
486 (buffer[0] != 0x00 || buffer[1] != 0x00))
494 pr_warn(
"failed to init the device\n");
497 static int applesmc_init_index(
struct applesmc_registers *
s)
505 s->index = kcalloc(s->temp_count,
sizeof(s->index[0]),
GFP_KERNEL);
509 for (i = s->temp_begin; i < s->temp_end; i++) {
510 entry = applesmc_get_entry_by_index(i);
515 s->index[s->index_count++] = entry->
key;
524 static int applesmc_init_smcreg_try(
void)
526 struct applesmc_registers *s = &smcreg;
527 bool left_light_sensor, right_light_sensor;
531 if (s->init_complete)
534 ret = read_register_count(&s->key_count);
539 s->cache = kcalloc(s->key_count,
sizeof(*s->cache),
GFP_KERNEL);
546 s->fan_count = tmp[0];
548 ret = applesmc_get_lower_bound(&s->temp_begin,
"T");
551 ret = applesmc_get_lower_bound(&s->temp_end,
"U");
554 s->temp_count = s->temp_end - s->temp_begin;
556 ret = applesmc_init_index(s);
569 ret = applesmc_has_key(
BACKLIGHT_KEY, &s->has_key_backlight);
573 s->num_light_sensors = left_light_sensor + right_light_sensor;
574 s->init_complete =
true;
576 pr_info(
"key=%d fan=%d temp=%d index=%d acc=%d lux=%d kbd=%d\n",
577 s->key_count, s->fan_count, s->temp_count, s->index_count,
578 s->has_accelerometer,
579 s->num_light_sensors,
580 s->has_key_backlight);
585 static void applesmc_destroy_smcreg(
void)
591 smcreg.init_complete =
false;
600 static int applesmc_init_smcreg(
void)
605 ret = applesmc_init_smcreg_try();
608 pr_info(
"init_smcreg() took %d ms\n", ms);
614 applesmc_destroy_smcreg();
624 ret = applesmc_init_smcreg();
628 applesmc_device_init();
634 static int applesmc_pm_resume(
struct device *dev)
636 if (smcreg.has_key_backlight)
642 static int applesmc_pm_restore(
struct device *dev)
644 applesmc_device_init();
645 return applesmc_pm_resume(dev);
648 static const struct dev_pm_ops applesmc_pm_ops = {
649 .resume = applesmc_pm_resume,
650 .restore = applesmc_pm_restore,
654 .probe = applesmc_probe,
658 .pm = &applesmc_pm_ops,
666 static void applesmc_calibrate(
void)
673 static void applesmc_idev_poll(
struct input_polled_dev *dev)
675 struct input_dev *
idev = dev->input;
684 input_report_abs(idev,
ABS_X, x - rest_x);
685 input_report_abs(idev,
ABS_Y, y - rest_y);
732 return PTR_ERR(entry);
735 data_length = entry->
len;
736 pr_info(
"light sensor data length set to %d\n", data_length);
741 if (data_length == 10) {
759 static ssize_t applesmc_show_sensor_label(
struct device *dev,
762 const char *key = smcreg.index[
to_index(devattr)];
771 const char *key = smcreg.index[
to_index(devattr)];
776 ret = applesmc_read_s16(key, &value);
780 temp = 250 * (value >> 6);
789 unsigned int speed = 0;
795 ret = applesmc_read_key(newkey, buffer, 2);
796 speed = ((buffer[0] << 8 | buffer[1]) >> 2);
806 const char *sysfsbuf,
size_t count)
813 if (kstrtoul(sysfsbuf, 10, &speed) < 0 || speed >= 0x4000)
818 buffer[0] = (speed >> 6) & 0xff;
819 buffer[1] = (speed << 2) & 0xff;
820 ret = applesmc_write_key(newkey, buffer, 2);
836 manual = ((buffer[0] << 8 | buffer[1]) >>
to_index(attr)) & 0x01;
846 const char *sysfsbuf,
size_t count)
853 if (kstrtoul(sysfsbuf, 10, &input) < 0)
857 val = (buffer[0] << 8 | buffer[1]);
862 val = val | (0x01 <<
to_index(attr));
864 val = val & ~(0x01 <<
to_index(attr));
866 buffer[0] = (val >> 8) & 0xFF;
867 buffer[1] = val & 0xFF;
878 static ssize_t applesmc_show_fan_position(
struct device *dev,
887 ret = applesmc_read_key(newkey, buffer, 16);
905 applesmc_calibrate();
914 static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
916 static void applesmc_brightness_set(
struct led_classdev *led_cdev,
921 backlight_state[0] =
value;
922 ret =
queue_work(applesmc_led_wq, &backlight_work);
936 count = ((
u32)buffer[0]<<24) + ((
u32)buffer[1]<<16) +
937 ((
u32)buffer[2]<<8) + buffer[3];
945 static ssize_t applesmc_key_at_index_read_show(
struct device *dev,
951 entry = applesmc_get_entry_by_index(key_at_index);
953 return PTR_ERR(entry);
954 ret = applesmc_read_entry(entry, sysfsbuf, entry->
len);
961 static ssize_t applesmc_key_at_index_data_length_show(
struct device *dev,
966 entry = applesmc_get_entry_by_index(key_at_index);
968 return PTR_ERR(entry);
973 static ssize_t applesmc_key_at_index_type_show(
struct device *dev,
978 entry = applesmc_get_entry_by_index(key_at_index);
980 return PTR_ERR(entry);
985 static ssize_t applesmc_key_at_index_name_show(
struct device *dev,
990 entry = applesmc_get_entry_by_index(key_at_index);
992 return PTR_ERR(entry);
997 static ssize_t applesmc_key_at_index_show(
struct device *dev,
1003 static ssize_t applesmc_key_at_index_store(
struct device *dev,
1006 unsigned long newkey;
1008 if (kstrtoul(sysfsbuf, 10, &newkey) < 0
1009 || newkey >= smcreg.key_count)
1012 key_at_index = newkey;
1017 .name =
"smc::kbd_backlight",
1018 .default_trigger =
"nand-disk",
1019 .brightness_set = applesmc_brightness_set,
1023 {
"name", applesmc_name_show },
1024 {
"key_count", applesmc_key_count_show },
1025 {
"key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store },
1026 {
"key_at_index_name", applesmc_key_at_index_name_show },
1027 {
"key_at_index_type", applesmc_key_at_index_type_show },
1028 {
"key_at_index_data_length", applesmc_key_at_index_data_length_show },
1029 {
"key_at_index_data", applesmc_key_at_index_read_show },
1034 {
"position", applesmc_position_show },
1035 {
"calibrate", applesmc_calibrate_show, applesmc_calibrate_store },
1040 {
"light", applesmc_light_show },
1045 {
"fan%d_label", applesmc_show_fan_position },
1046 {
"fan%d_input", applesmc_show_fan_speed,
NULL, 0 },
1047 {
"fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 },
1048 {
"fan%d_max", applesmc_show_fan_speed,
NULL, 2 },
1049 {
"fan%d_safe", applesmc_show_fan_speed,
NULL, 3 },
1050 {
"fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 },
1051 {
"fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual },
1056 {
"temp%d_label", applesmc_show_sensor_label },
1057 {
"temp%d_input", applesmc_show_temperature },
1071 for (grp = groups; grp->
nodes; grp++) {
1072 for (node = grp->
nodes; node->
sda.dev_attr.attr.name; node++)
1074 &node->
sda.dev_attr.attr);
1090 for (grp = groups; grp->
format; grp++) {
1096 for (i = 0; i < num; i++) {
1099 node->
sda.index = (grp->
option << 16) | (i & 0xffff);
1100 node->
sda.dev_attr.show = grp->
show;
1101 node->
sda.dev_attr.store = grp->
store;
1102 attr = &node->
sda.dev_attr.attr;
1116 applesmc_destroy_nodes(groups);
1121 static int applesmc_create_accelerometer(
void)
1123 struct input_dev *
idev;
1126 if (!smcreg.has_accelerometer)
1129 ret = applesmc_create_nodes(accelerometer_group, 1);
1134 if (!applesmc_idev) {
1139 applesmc_idev->poll = applesmc_idev_poll;
1143 applesmc_calibrate();
1146 idev = applesmc_idev->input;
1147 idev->name =
"applesmc";
1149 idev->dev.parent = &pdev->
dev;
1151 input_set_abs_params(idev,
ABS_X,
1153 input_set_abs_params(idev,
ABS_Y,
1166 applesmc_destroy_nodes(accelerometer_group);
1169 pr_warn(
"driver init failed (ret=%d)!\n", ret);
1174 static void applesmc_release_accelerometer(
void)
1176 if (!smcreg.has_accelerometer)
1180 applesmc_destroy_nodes(accelerometer_group);
1183 static int applesmc_create_light_sensor(
void)
1185 if (!smcreg.num_light_sensors)
1187 return applesmc_create_nodes(light_sensor_group, 1);
1190 static void applesmc_release_light_sensor(
void)
1192 if (!smcreg.num_light_sensors)
1194 applesmc_destroy_nodes(light_sensor_group);
1197 static int applesmc_create_key_backlight(
void)
1199 if (!smcreg.has_key_backlight)
1202 if (!applesmc_led_wq)
1207 static void applesmc_release_key_backlight(
void)
1209 if (!smcreg.has_key_backlight)
1215 static int applesmc_dmi_match(
const struct dmi_system_id *
id)
1225 { applesmc_dmi_match,
"Apple MacBook Air", {
1229 { applesmc_dmi_match,
"Apple MacBook Pro", {
1233 { applesmc_dmi_match,
"Apple MacBook", {
1237 { applesmc_dmi_match,
"Apple Macmini", {
1241 { applesmc_dmi_match,
"Apple MacPro", {
1245 { applesmc_dmi_match,
"Apple iMac", {
1252 static int __init applesmc_init(
void)
1257 pr_warn(
"supported laptop not found!\n");
1275 ret = PTR_ERR(pdev);
1280 ret = applesmc_init_smcreg();
1284 ret = applesmc_create_nodes(info_group, 1);
1288 ret = applesmc_create_nodes(fan_group, smcreg.fan_count);
1292 ret = applesmc_create_nodes(temp_group, smcreg.index_count);
1296 ret = applesmc_create_accelerometer();
1298 goto out_temperature;
1300 ret = applesmc_create_light_sensor();
1302 goto out_accelerometer;
1304 ret = applesmc_create_key_backlight();
1306 goto out_light_sysfs;
1309 if (IS_ERR(hwmon_dev)) {
1310 ret = PTR_ERR(hwmon_dev);
1311 goto out_light_ledclass;
1317 applesmc_release_key_backlight();
1319 applesmc_release_light_sensor();
1321 applesmc_release_accelerometer();
1323 applesmc_destroy_nodes(temp_group);
1325 applesmc_destroy_nodes(fan_group);
1327 applesmc_destroy_nodes(info_group);
1329 applesmc_destroy_smcreg();
1337 pr_warn(
"driver init failed (ret=%d)!\n", ret);
1341 static void __exit applesmc_exit(
void)
1344 applesmc_release_key_backlight();
1345 applesmc_release_light_sensor();
1346 applesmc_release_accelerometer();
1347 applesmc_destroy_nodes(temp_group);
1348 applesmc_destroy_nodes(fan_group);
1349 applesmc_destroy_nodes(info_group);
1350 applesmc_destroy_smcreg();