109308562SRahul Rameshbabu // SPDX-License-Identifier: GPL-2.0-or-later
209308562SRahul Rameshbabu /*
309308562SRahul Rameshbabu  *  Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
409308562SRahul Rameshbabu  *
509308562SRahul Rameshbabu  *  HID driver for NVIDIA SHIELD peripherals.
609308562SRahul Rameshbabu  */
709308562SRahul Rameshbabu 
809308562SRahul Rameshbabu #include <linux/hid.h>
93ab196f8SRahul Rameshbabu #include <linux/idr.h>
1009308562SRahul Rameshbabu #include <linux/input-event-codes.h>
1109308562SRahul Rameshbabu #include <linux/input.h>
123ab196f8SRahul Rameshbabu #include <linux/jiffies.h>
13f88af60eSRahul Rameshbabu #include <linux/leds.h>
1409308562SRahul Rameshbabu #include <linux/module.h>
153ab196f8SRahul Rameshbabu #include <linux/power_supply.h>
1609308562SRahul Rameshbabu #include <linux/spinlock.h>
173ab196f8SRahul Rameshbabu #include <linux/timer.h>
1809308562SRahul Rameshbabu #include <linux/workqueue.h>
1909308562SRahul Rameshbabu 
2009308562SRahul Rameshbabu #include "hid-ids.h"
2109308562SRahul Rameshbabu 
2209308562SRahul Rameshbabu #define NOT_INIT_STR "NOT INITIALIZED"
2313d02c69SRahul Rameshbabu #define android_map_key(c) hid_map_usage(hi, usage, bit, max, EV_KEY, (c))
2413d02c69SRahul Rameshbabu 
2513d02c69SRahul Rameshbabu enum {
2613d02c69SRahul Rameshbabu 	HID_USAGE_ANDROID_PLAYPAUSE_BTN = 0xcd, /* Double-tap volume slider */
2713d02c69SRahul Rameshbabu 	HID_USAGE_ANDROID_VOLUMEUP_BTN = 0xe9,
2813d02c69SRahul Rameshbabu 	HID_USAGE_ANDROID_VOLUMEDOWN_BTN = 0xea,
2913d02c69SRahul Rameshbabu 	HID_USAGE_ANDROID_SEARCH_BTN = 0x221, /* NVIDIA btn on Thunderstrike */
3013d02c69SRahul Rameshbabu 	HID_USAGE_ANDROID_HOME_BTN = 0x223,
3113d02c69SRahul Rameshbabu 	HID_USAGE_ANDROID_BACK_BTN = 0x224,
3213d02c69SRahul Rameshbabu };
3309308562SRahul Rameshbabu 
3409308562SRahul Rameshbabu enum {
3509308562SRahul Rameshbabu 	SHIELD_FW_VERSION_INITIALIZED = 0,
3609308562SRahul Rameshbabu 	SHIELD_BOARD_INFO_INITIALIZED,
373ab196f8SRahul Rameshbabu 	SHIELD_BATTERY_STATS_INITIALIZED,
383ab196f8SRahul Rameshbabu 	SHIELD_CHARGER_STATE_INITIALIZED,
3909308562SRahul Rameshbabu };
4009308562SRahul Rameshbabu 
4109308562SRahul Rameshbabu enum {
4209308562SRahul Rameshbabu 	THUNDERSTRIKE_FW_VERSION_UPDATE = 0,
4309308562SRahul Rameshbabu 	THUNDERSTRIKE_BOARD_INFO_UPDATE,
4409308562SRahul Rameshbabu 	THUNDERSTRIKE_HAPTICS_UPDATE,
45f88af60eSRahul Rameshbabu 	THUNDERSTRIKE_LED_UPDATE,
463ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_POWER_SUPPLY_STATS_UPDATE,
4709308562SRahul Rameshbabu };
4809308562SRahul Rameshbabu 
4909308562SRahul Rameshbabu enum {
5009308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_REPORT_SIZE = 33,
5109308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID = 0x4,
5209308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID = 0x3,
5309308562SRahul Rameshbabu };
5409308562SRahul Rameshbabu 
5509308562SRahul Rameshbabu enum {
5609308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION = 1,
57f88af60eSRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_LED = 6,
583ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_BATTERY,
5909308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO = 16,
6009308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_USB_INIT = 53,
6109308562SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_HAPTICS = 57,
623ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_HOSTCMD_ID_CHARGER,
633ab196f8SRahul Rameshbabu };
643ab196f8SRahul Rameshbabu 
653ab196f8SRahul Rameshbabu struct power_supply_dev {
663ab196f8SRahul Rameshbabu 	struct power_supply *psy;
673ab196f8SRahul Rameshbabu 	struct power_supply_desc desc;
683ab196f8SRahul Rameshbabu };
693ab196f8SRahul Rameshbabu 
703ab196f8SRahul Rameshbabu struct thunderstrike_psy_prop_values {
713ab196f8SRahul Rameshbabu 	int voltage_min;
723ab196f8SRahul Rameshbabu 	int voltage_now;
733ab196f8SRahul Rameshbabu 	int voltage_avg;
743ab196f8SRahul Rameshbabu 	int voltage_boot;
753ab196f8SRahul Rameshbabu 	int capacity;
763ab196f8SRahul Rameshbabu 	int status;
773ab196f8SRahul Rameshbabu 	int charge_type;
783ab196f8SRahul Rameshbabu 	int temp;
793ab196f8SRahul Rameshbabu };
803ab196f8SRahul Rameshbabu 
813ab196f8SRahul Rameshbabu static const enum power_supply_property thunderstrike_battery_props[] = {
823ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_STATUS,
833ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_CHARGE_TYPE,
843ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_PRESENT,
853ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_VOLTAGE_MIN,
863ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
873ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
883ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
893ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_VOLTAGE_AVG,
903ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_VOLTAGE_BOOT,
913ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_CAPACITY,
923ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_SCOPE,
933ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_TEMP,
943ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_TEMP_MIN,
953ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_TEMP_MAX,
963ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_TEMP_ALERT_MIN,
973ab196f8SRahul Rameshbabu 	POWER_SUPPLY_PROP_TEMP_ALERT_MAX,
9809308562SRahul Rameshbabu };
9909308562SRahul Rameshbabu 
100f88af60eSRahul Rameshbabu enum thunderstrike_led_state {
101f88af60eSRahul Rameshbabu 	THUNDERSTRIKE_LED_OFF = 1,
102f88af60eSRahul Rameshbabu 	THUNDERSTRIKE_LED_ON = 8,
103f88af60eSRahul Rameshbabu } __packed;
104f88af60eSRahul Rameshbabu static_assert(sizeof(enum thunderstrike_led_state) == 1);
105f88af60eSRahul Rameshbabu 
1063ab196f8SRahul Rameshbabu struct thunderstrike_hostcmd_battery {
1073ab196f8SRahul Rameshbabu 	__le16 voltage_avg;
1083ab196f8SRahul Rameshbabu 	u8 reserved_at_10;
1093ab196f8SRahul Rameshbabu 	__le16 thermistor;
1103ab196f8SRahul Rameshbabu 	__le16 voltage_min;
1113ab196f8SRahul Rameshbabu 	__le16 voltage_boot;
1123ab196f8SRahul Rameshbabu 	__le16 voltage_now;
1133ab196f8SRahul Rameshbabu 	u8 capacity;
1143ab196f8SRahul Rameshbabu } __packed;
1153ab196f8SRahul Rameshbabu 
1163ab196f8SRahul Rameshbabu enum thunderstrike_charger_type {
1173ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_TYPE_NONE = 0,
1183ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_TYPE_TRICKLE,
1193ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_TYPE_NORMAL,
1203ab196f8SRahul Rameshbabu } __packed;
1213ab196f8SRahul Rameshbabu static_assert(sizeof(enum thunderstrike_charger_type) == 1);
1223ab196f8SRahul Rameshbabu 
1233ab196f8SRahul Rameshbabu enum thunderstrike_charger_state {
1243ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_STATE_UNKNOWN = 0,
1253ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_STATE_DISABLED,
1263ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_STATE_CHARGING,
1273ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_STATE_FULL,
1283ab196f8SRahul Rameshbabu 	THUNDERSTRIKE_CHARGER_STATE_FAILED = 8,
1293ab196f8SRahul Rameshbabu } __packed;
1303ab196f8SRahul Rameshbabu static_assert(sizeof(enum thunderstrike_charger_state) == 1);
1313ab196f8SRahul Rameshbabu 
1323ab196f8SRahul Rameshbabu struct thunderstrike_hostcmd_charger {
1333ab196f8SRahul Rameshbabu 	u8 connected;
1343ab196f8SRahul Rameshbabu 	enum thunderstrike_charger_type type;
1353ab196f8SRahul Rameshbabu 	enum thunderstrike_charger_state state;
1363ab196f8SRahul Rameshbabu } __packed;
1373ab196f8SRahul Rameshbabu 
13809308562SRahul Rameshbabu struct thunderstrike_hostcmd_board_info {
13909308562SRahul Rameshbabu 	__le16 revision;
14009308562SRahul Rameshbabu 	__le16 serial[7];
1418bcf314bSRahul Rameshbabu } __packed;
14209308562SRahul Rameshbabu 
14309308562SRahul Rameshbabu struct thunderstrike_hostcmd_haptics {
14409308562SRahul Rameshbabu 	u8 motor_left;
14509308562SRahul Rameshbabu 	u8 motor_right;
1468bcf314bSRahul Rameshbabu } __packed;
14709308562SRahul Rameshbabu 
14809308562SRahul Rameshbabu struct thunderstrike_hostcmd_resp_report {
14909308562SRahul Rameshbabu 	u8 report_id; /* THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID */
15009308562SRahul Rameshbabu 	u8 cmd_id;
15109308562SRahul Rameshbabu 	u8 reserved_at_10;
15209308562SRahul Rameshbabu 
15309308562SRahul Rameshbabu 	union {
15409308562SRahul Rameshbabu 		struct thunderstrike_hostcmd_board_info board_info;
15509308562SRahul Rameshbabu 		struct thunderstrike_hostcmd_haptics motors;
15609308562SRahul Rameshbabu 		__le16 fw_version;
157f88af60eSRahul Rameshbabu 		enum thunderstrike_led_state led_state;
1583ab196f8SRahul Rameshbabu 		struct thunderstrike_hostcmd_battery battery;
1593ab196f8SRahul Rameshbabu 		struct thunderstrike_hostcmd_charger charger;
16009308562SRahul Rameshbabu 		u8 payload[30];
1618bcf314bSRahul Rameshbabu 	} __packed;
16209308562SRahul Rameshbabu } __packed;
16309308562SRahul Rameshbabu static_assert(sizeof(struct thunderstrike_hostcmd_resp_report) ==
16409308562SRahul Rameshbabu 	      THUNDERSTRIKE_HOSTCMD_REPORT_SIZE);
16509308562SRahul Rameshbabu 
16609308562SRahul Rameshbabu struct thunderstrike_hostcmd_req_report {
16709308562SRahul Rameshbabu 	u8 report_id; /* THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID */
16809308562SRahul Rameshbabu 	u8 cmd_id;
16909308562SRahul Rameshbabu 	u8 reserved_at_10;
17009308562SRahul Rameshbabu 
171f88af60eSRahul Rameshbabu 	union {
1728bcf314bSRahul Rameshbabu 		struct __packed {
173f88af60eSRahul Rameshbabu 			u8 update;
174f88af60eSRahul Rameshbabu 			enum thunderstrike_led_state state;
175f88af60eSRahul Rameshbabu 		} led;
1768bcf314bSRahul Rameshbabu 		struct __packed {
17709308562SRahul Rameshbabu 			u8 update;
17809308562SRahul Rameshbabu 			struct thunderstrike_hostcmd_haptics motors;
17909308562SRahul Rameshbabu 		} haptics;
1808bcf314bSRahul Rameshbabu 	} __packed;
18109308562SRahul Rameshbabu 	u8 reserved_at_30[27];
18209308562SRahul Rameshbabu } __packed;
18309308562SRahul Rameshbabu static_assert(sizeof(struct thunderstrike_hostcmd_req_report) ==
18409308562SRahul Rameshbabu 	      THUNDERSTRIKE_HOSTCMD_REPORT_SIZE);
18509308562SRahul Rameshbabu 
18609308562SRahul Rameshbabu /* Common struct for shield accessories. */
18709308562SRahul Rameshbabu struct shield_device {
18809308562SRahul Rameshbabu 	struct hid_device *hdev;
1893ab196f8SRahul Rameshbabu 	struct power_supply_dev battery_dev;
19009308562SRahul Rameshbabu 
19109308562SRahul Rameshbabu 	unsigned long initialized_flags;
19209308562SRahul Rameshbabu 	const char *codename;
19309308562SRahul Rameshbabu 	u16 fw_version;
19409308562SRahul Rameshbabu 	struct {
19509308562SRahul Rameshbabu 		u16 revision;
19609308562SRahul Rameshbabu 		char serial_number[15];
19709308562SRahul Rameshbabu 	} board_info;
19809308562SRahul Rameshbabu };
19909308562SRahul Rameshbabu 
2003ab196f8SRahul Rameshbabu /*
2013ab196f8SRahul Rameshbabu  * Non-trivial to uniquely identify Thunderstrike controllers at initialization
2023ab196f8SRahul Rameshbabu  * time. Use an ID allocator to help with this.
2033ab196f8SRahul Rameshbabu  */
2043ab196f8SRahul Rameshbabu static DEFINE_IDA(thunderstrike_ida);
2053ab196f8SRahul Rameshbabu 
20609308562SRahul Rameshbabu struct thunderstrike {
20709308562SRahul Rameshbabu 	struct shield_device base;
20809308562SRahul Rameshbabu 
2093ab196f8SRahul Rameshbabu 	int id;
2103ab196f8SRahul Rameshbabu 
21109308562SRahul Rameshbabu 	/* Sub-devices */
21209308562SRahul Rameshbabu 	struct input_dev *haptics_dev;
213f88af60eSRahul Rameshbabu 	struct led_classdev led_dev;
21409308562SRahul Rameshbabu 
21509308562SRahul Rameshbabu 	/* Resources */
21609308562SRahul Rameshbabu 	void *req_report_dmabuf;
21709308562SRahul Rameshbabu 	unsigned long update_flags;
21809308562SRahul Rameshbabu 	struct thunderstrike_hostcmd_haptics haptics_val;
21909308562SRahul Rameshbabu 	spinlock_t haptics_update_lock;
220f88af60eSRahul Rameshbabu 	u8 led_state : 1;
221f88af60eSRahul Rameshbabu 	enum thunderstrike_led_state led_value;
2223ab196f8SRahul Rameshbabu 	struct thunderstrike_psy_prop_values psy_stats;
2233ab196f8SRahul Rameshbabu 	spinlock_t psy_stats_lock;
2243ab196f8SRahul Rameshbabu 	struct timer_list psy_stats_timer;
22509308562SRahul Rameshbabu 	struct work_struct hostcmd_req_work;
22609308562SRahul Rameshbabu };
22709308562SRahul Rameshbabu 
thunderstrike_hostcmd_req_report_init(struct thunderstrike_hostcmd_req_report * report,u8 cmd_id)22809308562SRahul Rameshbabu static inline void thunderstrike_hostcmd_req_report_init(
22909308562SRahul Rameshbabu 	struct thunderstrike_hostcmd_req_report *report, u8 cmd_id)
23009308562SRahul Rameshbabu {
23109308562SRahul Rameshbabu 	memset(report, 0, sizeof(*report));
23209308562SRahul Rameshbabu 	report->report_id = THUNDERSTRIKE_HOSTCMD_REQ_REPORT_ID;
23309308562SRahul Rameshbabu 	report->cmd_id = cmd_id;
23409308562SRahul Rameshbabu }
23509308562SRahul Rameshbabu 
shield_strrev(char * dest,size_t len,u16 rev)23609308562SRahul Rameshbabu static inline void shield_strrev(char *dest, size_t len, u16 rev)
23709308562SRahul Rameshbabu {
23809308562SRahul Rameshbabu 	dest[0] = ('A' - 1) + (rev >> 8);
23909308562SRahul Rameshbabu 	snprintf(&dest[1], len - 1, "%02X", 0xff & rev);
24009308562SRahul Rameshbabu }
24109308562SRahul Rameshbabu 
shield_allocate_input_dev(struct hid_device * hdev,const char * name_suffix)24209308562SRahul Rameshbabu static struct input_dev *shield_allocate_input_dev(struct hid_device *hdev,
24309308562SRahul Rameshbabu 						   const char *name_suffix)
24409308562SRahul Rameshbabu {
24509308562SRahul Rameshbabu 	struct input_dev *idev;
24609308562SRahul Rameshbabu 
24709308562SRahul Rameshbabu 	idev = input_allocate_device();
24809308562SRahul Rameshbabu 	if (!idev)
24909308562SRahul Rameshbabu 		goto err_device;
25009308562SRahul Rameshbabu 
25109308562SRahul Rameshbabu 	idev->id.bustype = hdev->bus;
25209308562SRahul Rameshbabu 	idev->id.vendor = hdev->vendor;
25309308562SRahul Rameshbabu 	idev->id.product = hdev->product;
25409308562SRahul Rameshbabu 	idev->id.version = hdev->version;
25509308562SRahul Rameshbabu 	idev->uniq = hdev->uniq;
256197d3143SRahul Rameshbabu 	idev->name = devm_kasprintf(&hdev->dev, GFP_KERNEL, "%s %s", hdev->name,
25709308562SRahul Rameshbabu 				    name_suffix);
25809308562SRahul Rameshbabu 	if (!idev->name)
25909308562SRahul Rameshbabu 		goto err_name;
26009308562SRahul Rameshbabu 
26109308562SRahul Rameshbabu 	input_set_drvdata(idev, hdev);
26209308562SRahul Rameshbabu 
26309308562SRahul Rameshbabu 	return idev;
26409308562SRahul Rameshbabu 
26509308562SRahul Rameshbabu err_name:
26609308562SRahul Rameshbabu 	input_free_device(idev);
26709308562SRahul Rameshbabu err_device:
26809308562SRahul Rameshbabu 	return ERR_PTR(-ENOMEM);
26909308562SRahul Rameshbabu }
27009308562SRahul Rameshbabu 
shield_haptics_create(struct shield_device * dev,int (* play_effect)(struct input_dev *,void *,struct ff_effect *))27109308562SRahul Rameshbabu static struct input_dev *shield_haptics_create(
27209308562SRahul Rameshbabu 	struct shield_device *dev,
27309308562SRahul Rameshbabu 	int (*play_effect)(struct input_dev *, void *, struct ff_effect *))
27409308562SRahul Rameshbabu {
27509308562SRahul Rameshbabu 	struct input_dev *haptics;
27609308562SRahul Rameshbabu 	int ret;
27709308562SRahul Rameshbabu 
27809308562SRahul Rameshbabu 	if (!IS_ENABLED(CONFIG_NVIDIA_SHIELD_FF))
27909308562SRahul Rameshbabu 		return NULL;
28009308562SRahul Rameshbabu 
28109308562SRahul Rameshbabu 	haptics = shield_allocate_input_dev(dev->hdev, "Haptics");
28209308562SRahul Rameshbabu 	if (IS_ERR(haptics))
28309308562SRahul Rameshbabu 		return haptics;
28409308562SRahul Rameshbabu 
28509308562SRahul Rameshbabu 	input_set_capability(haptics, EV_FF, FF_RUMBLE);
286b3f20698SChen Ni 	ret = input_ff_create_memless(haptics, NULL, play_effect);
287b3f20698SChen Ni 	if (ret)
288b3f20698SChen Ni 		goto err;
28909308562SRahul Rameshbabu 
29009308562SRahul Rameshbabu 	ret = input_register_device(haptics);
29109308562SRahul Rameshbabu 	if (ret)
29209308562SRahul Rameshbabu 		goto err;
29309308562SRahul Rameshbabu 
29409308562SRahul Rameshbabu 	return haptics;
29509308562SRahul Rameshbabu 
29609308562SRahul Rameshbabu err:
29709308562SRahul Rameshbabu 	input_free_device(haptics);
29809308562SRahul Rameshbabu 	return ERR_PTR(ret);
29909308562SRahul Rameshbabu }
30009308562SRahul Rameshbabu 
thunderstrike_send_hostcmd_request(struct thunderstrike * ts)30109308562SRahul Rameshbabu static inline void thunderstrike_send_hostcmd_request(struct thunderstrike *ts)
30209308562SRahul Rameshbabu {
30309308562SRahul Rameshbabu 	struct thunderstrike_hostcmd_req_report *report = ts->req_report_dmabuf;
30409308562SRahul Rameshbabu 	struct shield_device *shield_dev = &ts->base;
30509308562SRahul Rameshbabu 	int ret;
30609308562SRahul Rameshbabu 
30709308562SRahul Rameshbabu 	ret = hid_hw_raw_request(shield_dev->hdev, report->report_id,
30809308562SRahul Rameshbabu 				 ts->req_report_dmabuf,
30909308562SRahul Rameshbabu 				 THUNDERSTRIKE_HOSTCMD_REPORT_SIZE,
31009308562SRahul Rameshbabu 				 HID_OUTPUT_REPORT, HID_REQ_SET_REPORT);
31109308562SRahul Rameshbabu 
31209308562SRahul Rameshbabu 	if (ret < 0) {
31309308562SRahul Rameshbabu 		hid_err(shield_dev->hdev,
31409308562SRahul Rameshbabu 			"Failed to output Thunderstrike HOSTCMD request HID report due to %pe\n",
31509308562SRahul Rameshbabu 			ERR_PTR(ret));
31609308562SRahul Rameshbabu 	}
31709308562SRahul Rameshbabu }
31809308562SRahul Rameshbabu 
thunderstrike_hostcmd_req_work_handler(struct work_struct * work)31909308562SRahul Rameshbabu static void thunderstrike_hostcmd_req_work_handler(struct work_struct *work)
32009308562SRahul Rameshbabu {
32109308562SRahul Rameshbabu 	struct thunderstrike *ts =
32209308562SRahul Rameshbabu 		container_of(work, struct thunderstrike, hostcmd_req_work);
32309308562SRahul Rameshbabu 	struct thunderstrike_hostcmd_req_report *report;
32409308562SRahul Rameshbabu 	unsigned long flags;
32509308562SRahul Rameshbabu 
32609308562SRahul Rameshbabu 	report = ts->req_report_dmabuf;
32709308562SRahul Rameshbabu 
32809308562SRahul Rameshbabu 	if (test_and_clear_bit(THUNDERSTRIKE_FW_VERSION_UPDATE, &ts->update_flags)) {
32909308562SRahul Rameshbabu 		thunderstrike_hostcmd_req_report_init(
33009308562SRahul Rameshbabu 			report, THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION);
33109308562SRahul Rameshbabu 		thunderstrike_send_hostcmd_request(ts);
33209308562SRahul Rameshbabu 	}
33309308562SRahul Rameshbabu 
334f88af60eSRahul Rameshbabu 	if (test_and_clear_bit(THUNDERSTRIKE_LED_UPDATE, &ts->update_flags)) {
335f88af60eSRahul Rameshbabu 		thunderstrike_hostcmd_req_report_init(report, THUNDERSTRIKE_HOSTCMD_ID_LED);
336f88af60eSRahul Rameshbabu 		report->led.update = 1;
337f88af60eSRahul Rameshbabu 		report->led.state = ts->led_value;
338f88af60eSRahul Rameshbabu 		thunderstrike_send_hostcmd_request(ts);
339f88af60eSRahul Rameshbabu 	}
340f88af60eSRahul Rameshbabu 
3413ab196f8SRahul Rameshbabu 	if (test_and_clear_bit(THUNDERSTRIKE_POWER_SUPPLY_STATS_UPDATE, &ts->update_flags)) {
3423ab196f8SRahul Rameshbabu 		thunderstrike_hostcmd_req_report_init(
3433ab196f8SRahul Rameshbabu 			report, THUNDERSTRIKE_HOSTCMD_ID_BATTERY);
3443ab196f8SRahul Rameshbabu 		thunderstrike_send_hostcmd_request(ts);
3453ab196f8SRahul Rameshbabu 
3463ab196f8SRahul Rameshbabu 		thunderstrike_hostcmd_req_report_init(
3473ab196f8SRahul Rameshbabu 			report, THUNDERSTRIKE_HOSTCMD_ID_CHARGER);
3483ab196f8SRahul Rameshbabu 		thunderstrike_send_hostcmd_request(ts);
3493ab196f8SRahul Rameshbabu 	}
3503ab196f8SRahul Rameshbabu 
35109308562SRahul Rameshbabu 	if (test_and_clear_bit(THUNDERSTRIKE_BOARD_INFO_UPDATE, &ts->update_flags)) {
35209308562SRahul Rameshbabu 		thunderstrike_hostcmd_req_report_init(
35309308562SRahul Rameshbabu 			report, THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO);
35409308562SRahul Rameshbabu 		thunderstrike_send_hostcmd_request(ts);
35509308562SRahul Rameshbabu 	}
35609308562SRahul Rameshbabu 
35709308562SRahul Rameshbabu 	if (test_and_clear_bit(THUNDERSTRIKE_HAPTICS_UPDATE, &ts->update_flags)) {
35809308562SRahul Rameshbabu 		thunderstrike_hostcmd_req_report_init(
35909308562SRahul Rameshbabu 			report, THUNDERSTRIKE_HOSTCMD_ID_HAPTICS);
36009308562SRahul Rameshbabu 
36109308562SRahul Rameshbabu 		report->haptics.update = 1;
36209308562SRahul Rameshbabu 		spin_lock_irqsave(&ts->haptics_update_lock, flags);
36309308562SRahul Rameshbabu 		report->haptics.motors = ts->haptics_val;
36409308562SRahul Rameshbabu 		spin_unlock_irqrestore(&ts->haptics_update_lock, flags);
36509308562SRahul Rameshbabu 
36609308562SRahul Rameshbabu 		thunderstrike_send_hostcmd_request(ts);
36709308562SRahul Rameshbabu 	}
36809308562SRahul Rameshbabu }
36909308562SRahul Rameshbabu 
thunderstrike_request_firmware_version(struct thunderstrike * ts)37009308562SRahul Rameshbabu static inline void thunderstrike_request_firmware_version(struct thunderstrike *ts)
37109308562SRahul Rameshbabu {
37209308562SRahul Rameshbabu 	set_bit(THUNDERSTRIKE_FW_VERSION_UPDATE, &ts->update_flags);
37309308562SRahul Rameshbabu 	schedule_work(&ts->hostcmd_req_work);
37409308562SRahul Rameshbabu }
37509308562SRahul Rameshbabu 
thunderstrike_request_board_info(struct thunderstrike * ts)37609308562SRahul Rameshbabu static inline void thunderstrike_request_board_info(struct thunderstrike *ts)
37709308562SRahul Rameshbabu {
37809308562SRahul Rameshbabu 	set_bit(THUNDERSTRIKE_BOARD_INFO_UPDATE, &ts->update_flags);
37909308562SRahul Rameshbabu 	schedule_work(&ts->hostcmd_req_work);
38009308562SRahul Rameshbabu }
38109308562SRahul Rameshbabu 
38209308562SRahul Rameshbabu static inline int
thunderstrike_update_haptics(struct thunderstrike * ts,struct thunderstrike_hostcmd_haptics * motors)38309308562SRahul Rameshbabu thunderstrike_update_haptics(struct thunderstrike *ts,
38409308562SRahul Rameshbabu 			     struct thunderstrike_hostcmd_haptics *motors)
38509308562SRahul Rameshbabu {
38609308562SRahul Rameshbabu 	unsigned long flags;
38709308562SRahul Rameshbabu 
38809308562SRahul Rameshbabu 	spin_lock_irqsave(&ts->haptics_update_lock, flags);
38909308562SRahul Rameshbabu 	ts->haptics_val = *motors;
39009308562SRahul Rameshbabu 	spin_unlock_irqrestore(&ts->haptics_update_lock, flags);
39109308562SRahul Rameshbabu 
39209308562SRahul Rameshbabu 	set_bit(THUNDERSTRIKE_HAPTICS_UPDATE, &ts->update_flags);
39309308562SRahul Rameshbabu 	schedule_work(&ts->hostcmd_req_work);
39409308562SRahul Rameshbabu 
39509308562SRahul Rameshbabu 	return 0;
39609308562SRahul Rameshbabu }
39709308562SRahul Rameshbabu 
thunderstrike_play_effect(struct input_dev * idev,void * data,struct ff_effect * effect)39809308562SRahul Rameshbabu static int thunderstrike_play_effect(struct input_dev *idev, void *data,
39909308562SRahul Rameshbabu 				     struct ff_effect *effect)
40009308562SRahul Rameshbabu {
40109308562SRahul Rameshbabu 	struct hid_device *hdev = input_get_drvdata(idev);
40209308562SRahul Rameshbabu 	struct thunderstrike_hostcmd_haptics motors;
40309308562SRahul Rameshbabu 	struct shield_device *shield_dev;
40409308562SRahul Rameshbabu 	struct thunderstrike *ts;
40509308562SRahul Rameshbabu 
40609308562SRahul Rameshbabu 	if (effect->type != FF_RUMBLE)
40709308562SRahul Rameshbabu 		return 0;
40809308562SRahul Rameshbabu 
40909308562SRahul Rameshbabu 	shield_dev = hid_get_drvdata(hdev);
41009308562SRahul Rameshbabu 	ts = container_of(shield_dev, struct thunderstrike, base);
41109308562SRahul Rameshbabu 
41209308562SRahul Rameshbabu 	/* Thunderstrike motor values range from 0 to 32 inclusively */
41309308562SRahul Rameshbabu 	motors.motor_left = effect->u.rumble.strong_magnitude / 2047;
41409308562SRahul Rameshbabu 	motors.motor_right = effect->u.rumble.weak_magnitude / 2047;
41509308562SRahul Rameshbabu 
41609308562SRahul Rameshbabu 	hid_dbg(hdev, "Thunderstrike FF_RUMBLE request, left: %u right: %u\n",
41709308562SRahul Rameshbabu 		motors.motor_left, motors.motor_right);
41809308562SRahul Rameshbabu 
41909308562SRahul Rameshbabu 	return thunderstrike_update_haptics(ts, &motors);
42009308562SRahul Rameshbabu }
42109308562SRahul Rameshbabu 
422f88af60eSRahul Rameshbabu static enum led_brightness
thunderstrike_led_get_brightness(struct led_classdev * led)423f88af60eSRahul Rameshbabu thunderstrike_led_get_brightness(struct led_classdev *led)
424f88af60eSRahul Rameshbabu {
425f88af60eSRahul Rameshbabu 	struct hid_device *hdev = to_hid_device(led->dev->parent);
426f88af60eSRahul Rameshbabu 	struct shield_device *shield_dev = hid_get_drvdata(hdev);
427f88af60eSRahul Rameshbabu 	struct thunderstrike *ts;
428f88af60eSRahul Rameshbabu 
429f88af60eSRahul Rameshbabu 	ts = container_of(shield_dev, struct thunderstrike, base);
430f88af60eSRahul Rameshbabu 
431f88af60eSRahul Rameshbabu 	return ts->led_state;
432f88af60eSRahul Rameshbabu }
433f88af60eSRahul Rameshbabu 
thunderstrike_led_set_brightness(struct led_classdev * led,enum led_brightness value)434f88af60eSRahul Rameshbabu static void thunderstrike_led_set_brightness(struct led_classdev *led,
435f88af60eSRahul Rameshbabu 					    enum led_brightness value)
436f88af60eSRahul Rameshbabu {
437f88af60eSRahul Rameshbabu 	struct hid_device *hdev = to_hid_device(led->dev->parent);
438f88af60eSRahul Rameshbabu 	struct shield_device *shield_dev = hid_get_drvdata(hdev);
439f88af60eSRahul Rameshbabu 	struct thunderstrike *ts;
440f88af60eSRahul Rameshbabu 
441f88af60eSRahul Rameshbabu 	ts = container_of(shield_dev, struct thunderstrike, base);
442f88af60eSRahul Rameshbabu 
443f88af60eSRahul Rameshbabu 	switch (value) {
444f88af60eSRahul Rameshbabu 	case LED_OFF:
445f88af60eSRahul Rameshbabu 		ts->led_value = THUNDERSTRIKE_LED_OFF;
446f88af60eSRahul Rameshbabu 		break;
447f88af60eSRahul Rameshbabu 	default:
448f88af60eSRahul Rameshbabu 		ts->led_value = THUNDERSTRIKE_LED_ON;
449f88af60eSRahul Rameshbabu 		break;
450f88af60eSRahul Rameshbabu 	}
451f88af60eSRahul Rameshbabu 
452f88af60eSRahul Rameshbabu 	set_bit(THUNDERSTRIKE_LED_UPDATE, &ts->update_flags);
453f88af60eSRahul Rameshbabu 	schedule_work(&ts->hostcmd_req_work);
454f88af60eSRahul Rameshbabu }
455f88af60eSRahul Rameshbabu 
thunderstrike_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)4563ab196f8SRahul Rameshbabu static int thunderstrike_battery_get_property(struct power_supply *psy,
4573ab196f8SRahul Rameshbabu 					      enum power_supply_property psp,
4583ab196f8SRahul Rameshbabu 					      union power_supply_propval *val)
4593ab196f8SRahul Rameshbabu {
4603ab196f8SRahul Rameshbabu 	struct shield_device *shield_dev = power_supply_get_drvdata(psy);
4613ab196f8SRahul Rameshbabu 	struct thunderstrike_psy_prop_values prop_values;
4623ab196f8SRahul Rameshbabu 	struct thunderstrike *ts;
4633ab196f8SRahul Rameshbabu 	int ret = 0;
4643ab196f8SRahul Rameshbabu 
4653ab196f8SRahul Rameshbabu 	ts = container_of(shield_dev, struct thunderstrike, base);
4663ab196f8SRahul Rameshbabu 	spin_lock(&ts->psy_stats_lock);
4673ab196f8SRahul Rameshbabu 	prop_values = ts->psy_stats;
4683ab196f8SRahul Rameshbabu 	spin_unlock(&ts->psy_stats_lock);
4693ab196f8SRahul Rameshbabu 
4703ab196f8SRahul Rameshbabu 	switch (psp) {
4713ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_STATUS:
4723ab196f8SRahul Rameshbabu 		val->intval = prop_values.status;
4733ab196f8SRahul Rameshbabu 		break;
4743ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
4753ab196f8SRahul Rameshbabu 		val->intval = prop_values.charge_type;
4763ab196f8SRahul Rameshbabu 		break;
4773ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_PRESENT:
4783ab196f8SRahul Rameshbabu 		val->intval = 1;
4793ab196f8SRahul Rameshbabu 		break;
4803ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_VOLTAGE_MIN:
4813ab196f8SRahul Rameshbabu 		val->intval = prop_values.voltage_min;
4823ab196f8SRahul Rameshbabu 		break;
4833ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
4843ab196f8SRahul Rameshbabu 		val->intval = 2900000; /* 2.9 V */
4853ab196f8SRahul Rameshbabu 		break;
4863ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
4873ab196f8SRahul Rameshbabu 		val->intval = 2200000; /* 2.2 V */
4883ab196f8SRahul Rameshbabu 		break;
4893ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
4903ab196f8SRahul Rameshbabu 		val->intval = prop_values.voltage_now;
4913ab196f8SRahul Rameshbabu 		break;
4923ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_VOLTAGE_AVG:
4933ab196f8SRahul Rameshbabu 		val->intval = prop_values.voltage_avg;
4943ab196f8SRahul Rameshbabu 		break;
4953ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_VOLTAGE_BOOT:
4963ab196f8SRahul Rameshbabu 		val->intval = prop_values.voltage_boot;
4973ab196f8SRahul Rameshbabu 		break;
4983ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_CAPACITY:
4993ab196f8SRahul Rameshbabu 		val->intval = prop_values.capacity;
5003ab196f8SRahul Rameshbabu 		break;
5013ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_SCOPE:
5023ab196f8SRahul Rameshbabu 		val->intval = POWER_SUPPLY_SCOPE_DEVICE;
5033ab196f8SRahul Rameshbabu 		break;
5043ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_TEMP:
5053ab196f8SRahul Rameshbabu 		val->intval = prop_values.temp;
5063ab196f8SRahul Rameshbabu 		break;
5073ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_TEMP_MIN:
5083ab196f8SRahul Rameshbabu 		val->intval = 0; /* 0 C */
5093ab196f8SRahul Rameshbabu 		break;
5103ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_TEMP_MAX:
5113ab196f8SRahul Rameshbabu 		val->intval = 400; /* 40 C */
5123ab196f8SRahul Rameshbabu 		break;
5133ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_TEMP_ALERT_MIN:
5143ab196f8SRahul Rameshbabu 		val->intval = 15; /* 1.5 C */
5153ab196f8SRahul Rameshbabu 		break;
5163ab196f8SRahul Rameshbabu 	case POWER_SUPPLY_PROP_TEMP_ALERT_MAX:
5173ab196f8SRahul Rameshbabu 		val->intval = 380; /* 38 C */
5183ab196f8SRahul Rameshbabu 		break;
5193ab196f8SRahul Rameshbabu 	default:
5203ab196f8SRahul Rameshbabu 		ret = -EINVAL;
5213ab196f8SRahul Rameshbabu 		break;
5223ab196f8SRahul Rameshbabu 	}
5233ab196f8SRahul Rameshbabu 
5243ab196f8SRahul Rameshbabu 	return ret;
5253ab196f8SRahul Rameshbabu }
5263ab196f8SRahul Rameshbabu 
thunderstrike_request_psy_stats(struct thunderstrike * ts)5273ab196f8SRahul Rameshbabu static inline void thunderstrike_request_psy_stats(struct thunderstrike *ts)
5283ab196f8SRahul Rameshbabu {
5293ab196f8SRahul Rameshbabu 	set_bit(THUNDERSTRIKE_POWER_SUPPLY_STATS_UPDATE, &ts->update_flags);
5303ab196f8SRahul Rameshbabu 	schedule_work(&ts->hostcmd_req_work);
5313ab196f8SRahul Rameshbabu }
5323ab196f8SRahul Rameshbabu 
thunderstrike_psy_stats_timer_handler(struct timer_list * timer)5333ab196f8SRahul Rameshbabu static void thunderstrike_psy_stats_timer_handler(struct timer_list *timer)
5343ab196f8SRahul Rameshbabu {
5353ab196f8SRahul Rameshbabu 	struct thunderstrike *ts =
5363ab196f8SRahul Rameshbabu 		container_of(timer, struct thunderstrike, psy_stats_timer);
5373ab196f8SRahul Rameshbabu 
5383ab196f8SRahul Rameshbabu 	thunderstrike_request_psy_stats(ts);
5393ab196f8SRahul Rameshbabu 	/* Query battery statistics from device every five minutes */
5403ab196f8SRahul Rameshbabu 	mod_timer(timer, jiffies + 300 * HZ);
5413ab196f8SRahul Rameshbabu }
5423ab196f8SRahul Rameshbabu 
54309308562SRahul Rameshbabu static void
thunderstrike_parse_fw_version_payload(struct shield_device * shield_dev,__le16 fw_version)54409308562SRahul Rameshbabu thunderstrike_parse_fw_version_payload(struct shield_device *shield_dev,
54509308562SRahul Rameshbabu 				       __le16 fw_version)
54609308562SRahul Rameshbabu {
54709308562SRahul Rameshbabu 	shield_dev->fw_version = le16_to_cpu(fw_version);
54809308562SRahul Rameshbabu 
54909308562SRahul Rameshbabu 	set_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags);
55009308562SRahul Rameshbabu 
55109308562SRahul Rameshbabu 	hid_dbg(shield_dev->hdev, "Thunderstrike firmware version 0x%04X\n",
55209308562SRahul Rameshbabu 		shield_dev->fw_version);
55309308562SRahul Rameshbabu }
55409308562SRahul Rameshbabu 
55509308562SRahul Rameshbabu static void
thunderstrike_parse_board_info_payload(struct shield_device * shield_dev,struct thunderstrike_hostcmd_board_info * board_info)55609308562SRahul Rameshbabu thunderstrike_parse_board_info_payload(struct shield_device *shield_dev,
55709308562SRahul Rameshbabu 				       struct thunderstrike_hostcmd_board_info *board_info)
55809308562SRahul Rameshbabu {
55909308562SRahul Rameshbabu 	char board_revision_str[4];
56009308562SRahul Rameshbabu 	int i;
56109308562SRahul Rameshbabu 
56209308562SRahul Rameshbabu 	shield_dev->board_info.revision = le16_to_cpu(board_info->revision);
56309308562SRahul Rameshbabu 	for (i = 0; i < 7; ++i) {
56409308562SRahul Rameshbabu 		u16 val = le16_to_cpu(board_info->serial[i]);
56509308562SRahul Rameshbabu 
56609308562SRahul Rameshbabu 		shield_dev->board_info.serial_number[2 * i] = val & 0xFF;
56709308562SRahul Rameshbabu 		shield_dev->board_info.serial_number[2 * i + 1] = val >> 8;
56809308562SRahul Rameshbabu 	}
56909308562SRahul Rameshbabu 	shield_dev->board_info.serial_number[14] = '\0';
57009308562SRahul Rameshbabu 
57109308562SRahul Rameshbabu 	set_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags);
57209308562SRahul Rameshbabu 
57309308562SRahul Rameshbabu 	shield_strrev(board_revision_str, 4, shield_dev->board_info.revision);
57409308562SRahul Rameshbabu 	hid_dbg(shield_dev->hdev,
57509308562SRahul Rameshbabu 		"Thunderstrike BOARD_REVISION_%s (0x%04X) S/N: %s\n",
57609308562SRahul Rameshbabu 		board_revision_str, shield_dev->board_info.revision,
57709308562SRahul Rameshbabu 		shield_dev->board_info.serial_number);
57809308562SRahul Rameshbabu }
57909308562SRahul Rameshbabu 
58009308562SRahul Rameshbabu static inline void
thunderstrike_parse_haptics_payload(struct shield_device * shield_dev,struct thunderstrike_hostcmd_haptics * haptics)58109308562SRahul Rameshbabu thunderstrike_parse_haptics_payload(struct shield_device *shield_dev,
58209308562SRahul Rameshbabu 				    struct thunderstrike_hostcmd_haptics *haptics)
58309308562SRahul Rameshbabu {
58409308562SRahul Rameshbabu 	hid_dbg(shield_dev->hdev,
58509308562SRahul Rameshbabu 		"Thunderstrike haptics HOSTCMD response, left: %u right: %u\n",
58609308562SRahul Rameshbabu 		haptics->motor_left, haptics->motor_right);
58709308562SRahul Rameshbabu }
58809308562SRahul Rameshbabu 
589f88af60eSRahul Rameshbabu static void
thunderstrike_parse_led_payload(struct shield_device * shield_dev,enum thunderstrike_led_state led_state)590f88af60eSRahul Rameshbabu thunderstrike_parse_led_payload(struct shield_device *shield_dev,
591f88af60eSRahul Rameshbabu 				enum thunderstrike_led_state led_state)
592f88af60eSRahul Rameshbabu {
593f88af60eSRahul Rameshbabu 	struct thunderstrike *ts = container_of(shield_dev, struct thunderstrike, base);
594f88af60eSRahul Rameshbabu 
595f88af60eSRahul Rameshbabu 	switch (led_state) {
596f88af60eSRahul Rameshbabu 	case THUNDERSTRIKE_LED_OFF:
597f88af60eSRahul Rameshbabu 		ts->led_state = 0;
598f88af60eSRahul Rameshbabu 		break;
599f88af60eSRahul Rameshbabu 	case THUNDERSTRIKE_LED_ON:
600f88af60eSRahul Rameshbabu 		ts->led_state = 1;
601f88af60eSRahul Rameshbabu 		break;
602f88af60eSRahul Rameshbabu 	}
603f88af60eSRahul Rameshbabu 
604f88af60eSRahul Rameshbabu 	hid_dbg(shield_dev->hdev, "Thunderstrike led HOSTCMD response, 0x%02X\n", led_state);
605f88af60eSRahul Rameshbabu }
606f88af60eSRahul Rameshbabu 
thunderstrike_parse_battery_payload(struct shield_device * shield_dev,struct thunderstrike_hostcmd_battery * battery)6073ab196f8SRahul Rameshbabu static void thunderstrike_parse_battery_payload(
6083ab196f8SRahul Rameshbabu 	struct shield_device *shield_dev,
6093ab196f8SRahul Rameshbabu 	struct thunderstrike_hostcmd_battery *battery)
6103ab196f8SRahul Rameshbabu {
6113ab196f8SRahul Rameshbabu 	struct thunderstrike *ts = container_of(shield_dev, struct thunderstrike, base);
6123ab196f8SRahul Rameshbabu 	u16 hostcmd_voltage_boot = le16_to_cpu(battery->voltage_boot);
6133ab196f8SRahul Rameshbabu 	u16 hostcmd_voltage_avg = le16_to_cpu(battery->voltage_avg);
6143ab196f8SRahul Rameshbabu 	u16 hostcmd_voltage_min = le16_to_cpu(battery->voltage_min);
6153ab196f8SRahul Rameshbabu 	u16 hostcmd_voltage_now = le16_to_cpu(battery->voltage_now);
6163ab196f8SRahul Rameshbabu 	u16 hostcmd_thermistor = le16_to_cpu(battery->thermistor);
6173ab196f8SRahul Rameshbabu 	int voltage_boot, voltage_avg, voltage_min, voltage_now;
6183ab196f8SRahul Rameshbabu 	struct hid_device *hdev = shield_dev->hdev;
6193ab196f8SRahul Rameshbabu 	u8 capacity = battery->capacity;
6203ab196f8SRahul Rameshbabu 	int temp;
6213ab196f8SRahul Rameshbabu 
6223ab196f8SRahul Rameshbabu 	/* Convert thunderstrike device values to µV and tenths of degree Celsius */
6233ab196f8SRahul Rameshbabu 	voltage_boot = hostcmd_voltage_boot * 1000;
6243ab196f8SRahul Rameshbabu 	voltage_avg = hostcmd_voltage_avg * 1000;
6253ab196f8SRahul Rameshbabu 	voltage_min = hostcmd_voltage_min * 1000;
6263ab196f8SRahul Rameshbabu 	voltage_now = hostcmd_voltage_now * 1000;
6273ab196f8SRahul Rameshbabu 	temp = (1378 - (int)hostcmd_thermistor) * 10 / 19;
6283ab196f8SRahul Rameshbabu 
6293ab196f8SRahul Rameshbabu 	/* Copy converted values */
6303ab196f8SRahul Rameshbabu 	spin_lock(&ts->psy_stats_lock);
6313ab196f8SRahul Rameshbabu 	ts->psy_stats.voltage_boot = voltage_boot;
6323ab196f8SRahul Rameshbabu 	ts->psy_stats.voltage_avg = voltage_avg;
6333ab196f8SRahul Rameshbabu 	ts->psy_stats.voltage_min = voltage_min;
6343ab196f8SRahul Rameshbabu 	ts->psy_stats.voltage_now = voltage_now;
6353ab196f8SRahul Rameshbabu 	ts->psy_stats.capacity = capacity;
6363ab196f8SRahul Rameshbabu 	ts->psy_stats.temp = temp;
6373ab196f8SRahul Rameshbabu 	spin_unlock(&ts->psy_stats_lock);
6383ab196f8SRahul Rameshbabu 
6393ab196f8SRahul Rameshbabu 	set_bit(SHIELD_BATTERY_STATS_INITIALIZED, &shield_dev->initialized_flags);
6403ab196f8SRahul Rameshbabu 
6413ab196f8SRahul Rameshbabu 	hid_dbg(hdev,
6423ab196f8SRahul Rameshbabu 		"Thunderstrike battery HOSTCMD response, voltage_avg: %u voltage_now: %u\n",
6433ab196f8SRahul Rameshbabu 		hostcmd_voltage_avg, hostcmd_voltage_now);
6443ab196f8SRahul Rameshbabu 	hid_dbg(hdev,
6453ab196f8SRahul Rameshbabu 		"Thunderstrike battery HOSTCMD response, voltage_boot: %u voltage_min: %u\n",
6463ab196f8SRahul Rameshbabu 		hostcmd_voltage_boot, hostcmd_voltage_min);
6473ab196f8SRahul Rameshbabu 	hid_dbg(hdev,
6483ab196f8SRahul Rameshbabu 		"Thunderstrike battery HOSTCMD response, thermistor: %u\n",
6493ab196f8SRahul Rameshbabu 		hostcmd_thermistor);
6503ab196f8SRahul Rameshbabu 	hid_dbg(hdev,
6513ab196f8SRahul Rameshbabu 		"Thunderstrike battery HOSTCMD response, capacity: %u%%\n",
6523ab196f8SRahul Rameshbabu 		capacity);
6533ab196f8SRahul Rameshbabu }
6543ab196f8SRahul Rameshbabu 
thunderstrike_parse_charger_payload(struct shield_device * shield_dev,struct thunderstrike_hostcmd_charger * charger)6553ab196f8SRahul Rameshbabu static void thunderstrike_parse_charger_payload(
6563ab196f8SRahul Rameshbabu 	struct shield_device *shield_dev,
6573ab196f8SRahul Rameshbabu 	struct thunderstrike_hostcmd_charger *charger)
6583ab196f8SRahul Rameshbabu {
6593ab196f8SRahul Rameshbabu 	struct thunderstrike *ts = container_of(shield_dev, struct thunderstrike, base);
6603ab196f8SRahul Rameshbabu 	int charge_type = POWER_SUPPLY_CHARGE_TYPE_UNKNOWN;
6613ab196f8SRahul Rameshbabu 	struct hid_device *hdev = shield_dev->hdev;
6623ab196f8SRahul Rameshbabu 	int status = POWER_SUPPLY_STATUS_UNKNOWN;
6633ab196f8SRahul Rameshbabu 
6643ab196f8SRahul Rameshbabu 	switch (charger->type) {
6653ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_TYPE_NONE:
6663ab196f8SRahul Rameshbabu 		charge_type = POWER_SUPPLY_CHARGE_TYPE_NONE;
6673ab196f8SRahul Rameshbabu 		break;
6683ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_TYPE_TRICKLE:
6693ab196f8SRahul Rameshbabu 		charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
6703ab196f8SRahul Rameshbabu 		break;
6713ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_TYPE_NORMAL:
6723ab196f8SRahul Rameshbabu 		charge_type = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
6733ab196f8SRahul Rameshbabu 		break;
6743ab196f8SRahul Rameshbabu 	default:
6753ab196f8SRahul Rameshbabu 		hid_warn(hdev, "Unhandled Thunderstrike charger HOSTCMD type, %u\n",
6763ab196f8SRahul Rameshbabu 			 charger->type);
6773ab196f8SRahul Rameshbabu 		break;
6783ab196f8SRahul Rameshbabu 	}
6793ab196f8SRahul Rameshbabu 
6803ab196f8SRahul Rameshbabu 	switch (charger->state) {
6813ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_STATE_UNKNOWN:
6823ab196f8SRahul Rameshbabu 		status = POWER_SUPPLY_STATUS_UNKNOWN;
6833ab196f8SRahul Rameshbabu 		break;
6843ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_STATE_DISABLED:
6853ab196f8SRahul Rameshbabu 		/* Indicates charger is disconnected */
6863ab196f8SRahul Rameshbabu 		break;
6873ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_STATE_CHARGING:
6883ab196f8SRahul Rameshbabu 		status = POWER_SUPPLY_STATUS_CHARGING;
6893ab196f8SRahul Rameshbabu 		break;
6903ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_STATE_FULL:
6913ab196f8SRahul Rameshbabu 		status = POWER_SUPPLY_STATUS_FULL;
6923ab196f8SRahul Rameshbabu 		break;
6933ab196f8SRahul Rameshbabu 	case THUNDERSTRIKE_CHARGER_STATE_FAILED:
6943ab196f8SRahul Rameshbabu 		status = POWER_SUPPLY_STATUS_NOT_CHARGING;
6953ab196f8SRahul Rameshbabu 		hid_err(hdev, "Thunderstrike device failed to charge\n");
6963ab196f8SRahul Rameshbabu 		break;
6973ab196f8SRahul Rameshbabu 	default:
6983ab196f8SRahul Rameshbabu 		hid_warn(hdev, "Unhandled Thunderstrike charger HOSTCMD state, %u\n",
6993ab196f8SRahul Rameshbabu 			 charger->state);
7003ab196f8SRahul Rameshbabu 		break;
7013ab196f8SRahul Rameshbabu 	}
7023ab196f8SRahul Rameshbabu 
7033ab196f8SRahul Rameshbabu 	if (!charger->connected)
7043ab196f8SRahul Rameshbabu 		status = POWER_SUPPLY_STATUS_DISCHARGING;
7053ab196f8SRahul Rameshbabu 
7063ab196f8SRahul Rameshbabu 	spin_lock(&ts->psy_stats_lock);
7073ab196f8SRahul Rameshbabu 	ts->psy_stats.charge_type = charge_type;
7083ab196f8SRahul Rameshbabu 	ts->psy_stats.status = status;
7093ab196f8SRahul Rameshbabu 	spin_unlock(&ts->psy_stats_lock);
7103ab196f8SRahul Rameshbabu 
7113ab196f8SRahul Rameshbabu 	set_bit(SHIELD_CHARGER_STATE_INITIALIZED, &shield_dev->initialized_flags);
7123ab196f8SRahul Rameshbabu 
7133ab196f8SRahul Rameshbabu 	hid_dbg(hdev,
7143ab196f8SRahul Rameshbabu 		"Thunderstrike charger HOSTCMD response, connected: %u, type: %u, state: %u\n",
7153ab196f8SRahul Rameshbabu 		charger->connected, charger->type, charger->state);
7163ab196f8SRahul Rameshbabu }
7173ab196f8SRahul Rameshbabu 
thunderstrike_device_init_info(struct shield_device * shield_dev)7183ab196f8SRahul Rameshbabu static inline void thunderstrike_device_init_info(struct shield_device *shield_dev)
7193ab196f8SRahul Rameshbabu {
7203ab196f8SRahul Rameshbabu 	struct thunderstrike *ts =
7213ab196f8SRahul Rameshbabu 		container_of(shield_dev, struct thunderstrike, base);
7223ab196f8SRahul Rameshbabu 
7233ab196f8SRahul Rameshbabu 	if (!test_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags))
7243ab196f8SRahul Rameshbabu 		thunderstrike_request_firmware_version(ts);
7253ab196f8SRahul Rameshbabu 
7263ab196f8SRahul Rameshbabu 	if (!test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags))
7273ab196f8SRahul Rameshbabu 		thunderstrike_request_board_info(ts);
7283ab196f8SRahul Rameshbabu 
7293ab196f8SRahul Rameshbabu 	if (!test_bit(SHIELD_BATTERY_STATS_INITIALIZED, &shield_dev->initialized_flags) ||
7303ab196f8SRahul Rameshbabu 	    !test_bit(SHIELD_CHARGER_STATE_INITIALIZED, &shield_dev->initialized_flags))
7313ab196f8SRahul Rameshbabu 		thunderstrike_psy_stats_timer_handler(&ts->psy_stats_timer);
7323ab196f8SRahul Rameshbabu }
7333ab196f8SRahul Rameshbabu 
thunderstrike_parse_report(struct shield_device * shield_dev,struct hid_report * report,u8 * data,int size)73409308562SRahul Rameshbabu static int thunderstrike_parse_report(struct shield_device *shield_dev,
73509308562SRahul Rameshbabu 				      struct hid_report *report, u8 *data,
73609308562SRahul Rameshbabu 				      int size)
73709308562SRahul Rameshbabu {
73809308562SRahul Rameshbabu 	struct thunderstrike_hostcmd_resp_report *hostcmd_resp_report;
73909308562SRahul Rameshbabu 	struct hid_device *hdev = shield_dev->hdev;
74009308562SRahul Rameshbabu 
74109308562SRahul Rameshbabu 	switch (report->id) {
74209308562SRahul Rameshbabu 	case THUNDERSTRIKE_HOSTCMD_RESP_REPORT_ID:
74309308562SRahul Rameshbabu 		if (size != THUNDERSTRIKE_HOSTCMD_REPORT_SIZE) {
74409308562SRahul Rameshbabu 			hid_err(hdev,
74509308562SRahul Rameshbabu 				"Encountered Thunderstrike HOSTCMD HID report with unexpected size %d\n",
74609308562SRahul Rameshbabu 				size);
74709308562SRahul Rameshbabu 			return -EINVAL;
74809308562SRahul Rameshbabu 		}
74909308562SRahul Rameshbabu 
75009308562SRahul Rameshbabu 		hostcmd_resp_report =
75109308562SRahul Rameshbabu 			(struct thunderstrike_hostcmd_resp_report *)data;
75209308562SRahul Rameshbabu 
75309308562SRahul Rameshbabu 		switch (hostcmd_resp_report->cmd_id) {
75409308562SRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_FW_VERSION:
75509308562SRahul Rameshbabu 			thunderstrike_parse_fw_version_payload(
75609308562SRahul Rameshbabu 				shield_dev, hostcmd_resp_report->fw_version);
75709308562SRahul Rameshbabu 			break;
758f88af60eSRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_LED:
759f88af60eSRahul Rameshbabu 			thunderstrike_parse_led_payload(shield_dev, hostcmd_resp_report->led_state);
760f88af60eSRahul Rameshbabu 			break;
7613ab196f8SRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_BATTERY:
7623ab196f8SRahul Rameshbabu 			thunderstrike_parse_battery_payload(shield_dev,
7633ab196f8SRahul Rameshbabu 							    &hostcmd_resp_report->battery);
7643ab196f8SRahul Rameshbabu 			break;
76509308562SRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_BOARD_INFO:
76609308562SRahul Rameshbabu 			thunderstrike_parse_board_info_payload(
76709308562SRahul Rameshbabu 				shield_dev, &hostcmd_resp_report->board_info);
76809308562SRahul Rameshbabu 			break;
76909308562SRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_HAPTICS:
77009308562SRahul Rameshbabu 			thunderstrike_parse_haptics_payload(
77109308562SRahul Rameshbabu 				shield_dev, &hostcmd_resp_report->motors);
77209308562SRahul Rameshbabu 			break;
77309308562SRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_USB_INIT:
77409308562SRahul Rameshbabu 			/* May block HOSTCMD requests till received initially */
7753ab196f8SRahul Rameshbabu 			thunderstrike_device_init_info(shield_dev);
7763ab196f8SRahul Rameshbabu 			break;
7773ab196f8SRahul Rameshbabu 		case THUNDERSTRIKE_HOSTCMD_ID_CHARGER:
7783ab196f8SRahul Rameshbabu 			/* May block HOSTCMD requests till received initially */
7793ab196f8SRahul Rameshbabu 			thunderstrike_device_init_info(shield_dev);
7803ab196f8SRahul Rameshbabu 
7813ab196f8SRahul Rameshbabu 			thunderstrike_parse_charger_payload(
7823ab196f8SRahul Rameshbabu 				shield_dev, &hostcmd_resp_report->charger);
7833ab196f8SRahul Rameshbabu 			break;
78409308562SRahul Rameshbabu 		default:
78509308562SRahul Rameshbabu 			hid_warn(hdev,
78609308562SRahul Rameshbabu 				 "Unhandled Thunderstrike HOSTCMD id %d\n",
78709308562SRahul Rameshbabu 				 hostcmd_resp_report->cmd_id);
78809308562SRahul Rameshbabu 			return -ENOENT;
78909308562SRahul Rameshbabu 		}
79009308562SRahul Rameshbabu 
79109308562SRahul Rameshbabu 		break;
79209308562SRahul Rameshbabu 	default:
79309308562SRahul Rameshbabu 		return 0;
79409308562SRahul Rameshbabu 	}
79509308562SRahul Rameshbabu 
79609308562SRahul Rameshbabu 	return 0;
79709308562SRahul Rameshbabu }
79809308562SRahul Rameshbabu 
thunderstrike_led_create(struct thunderstrike * ts)799f88af60eSRahul Rameshbabu static inline int thunderstrike_led_create(struct thunderstrike *ts)
800f88af60eSRahul Rameshbabu {
801f88af60eSRahul Rameshbabu 	struct led_classdev *led = &ts->led_dev;
802f88af60eSRahul Rameshbabu 
80377fe1fedSRahul Rameshbabu 	led->name = devm_kasprintf(&ts->base.hdev->dev, GFP_KERNEL,
80477fe1fedSRahul Rameshbabu 				   "thunderstrike%d:blue:led", ts->id);
80583527a13SKunwu Chan 	if (!led->name)
80683527a13SKunwu Chan 		return -ENOMEM;
807f88af60eSRahul Rameshbabu 	led->max_brightness = 1;
808b07b6b27SChristophe JAILLET 	led->flags = LED_CORE_SUSPENDRESUME | LED_RETAIN_AT_SHUTDOWN;
809f88af60eSRahul Rameshbabu 	led->brightness_get = &thunderstrike_led_get_brightness;
810f88af60eSRahul Rameshbabu 	led->brightness_set = &thunderstrike_led_set_brightness;
811f88af60eSRahul Rameshbabu 
812f88af60eSRahul Rameshbabu 	return led_classdev_register(&ts->base.hdev->dev, led);
813f88af60eSRahul Rameshbabu }
814f88af60eSRahul Rameshbabu 
thunderstrike_psy_create(struct shield_device * shield_dev)8153ab196f8SRahul Rameshbabu static inline int thunderstrike_psy_create(struct shield_device *shield_dev)
8163ab196f8SRahul Rameshbabu {
8173ab196f8SRahul Rameshbabu 	struct thunderstrike *ts = container_of(shield_dev, struct thunderstrike, base);
8183ab196f8SRahul Rameshbabu 	struct power_supply_config psy_cfg = { .drv_data = shield_dev, };
8193ab196f8SRahul Rameshbabu 	struct hid_device *hdev = shield_dev->hdev;
8203ab196f8SRahul Rameshbabu 	int ret;
8213ab196f8SRahul Rameshbabu 
8223ab196f8SRahul Rameshbabu 	/*
8233ab196f8SRahul Rameshbabu 	 * Set an initial capacity and temperature value to avoid prematurely
8243ab196f8SRahul Rameshbabu 	 * triggering alerts. Will be replaced by values queried from initial
8253ab196f8SRahul Rameshbabu 	 * HOSTCMD requests.
8263ab196f8SRahul Rameshbabu 	 */
8273ab196f8SRahul Rameshbabu 	ts->psy_stats.capacity = 100;
8283ab196f8SRahul Rameshbabu 	ts->psy_stats.temp = 182;
8293ab196f8SRahul Rameshbabu 
8303ab196f8SRahul Rameshbabu 	shield_dev->battery_dev.desc.properties = thunderstrike_battery_props;
8313ab196f8SRahul Rameshbabu 	shield_dev->battery_dev.desc.num_properties =
8323ab196f8SRahul Rameshbabu 		ARRAY_SIZE(thunderstrike_battery_props);
8333ab196f8SRahul Rameshbabu 	shield_dev->battery_dev.desc.get_property = thunderstrike_battery_get_property;
8343ab196f8SRahul Rameshbabu 	shield_dev->battery_dev.desc.type = POWER_SUPPLY_TYPE_BATTERY;
8353ab196f8SRahul Rameshbabu 	shield_dev->battery_dev.desc.name =
8363ab196f8SRahul Rameshbabu 		devm_kasprintf(&ts->base.hdev->dev, GFP_KERNEL,
8373ab196f8SRahul Rameshbabu 			       "thunderstrike_%d", ts->id);
83883527a13SKunwu Chan 	if (!shield_dev->battery_dev.desc.name)
83983527a13SKunwu Chan 		return -ENOMEM;
8403ab196f8SRahul Rameshbabu 
8413ab196f8SRahul Rameshbabu 	shield_dev->battery_dev.psy = power_supply_register(
8423ab196f8SRahul Rameshbabu 		&hdev->dev, &shield_dev->battery_dev.desc, &psy_cfg);
8433ab196f8SRahul Rameshbabu 	if (IS_ERR(shield_dev->battery_dev.psy)) {
8443ab196f8SRahul Rameshbabu 		hid_err(hdev, "Failed to register Thunderstrike battery device\n");
8453ab196f8SRahul Rameshbabu 		return PTR_ERR(shield_dev->battery_dev.psy);
8463ab196f8SRahul Rameshbabu 	}
8473ab196f8SRahul Rameshbabu 
8483ab196f8SRahul Rameshbabu 	ret = power_supply_powers(shield_dev->battery_dev.psy, &hdev->dev);
8493ab196f8SRahul Rameshbabu 	if (ret) {
8503ab196f8SRahul Rameshbabu 		hid_err(hdev, "Failed to associate battery device to Thunderstrike\n");
8513ab196f8SRahul Rameshbabu 		goto err;
8523ab196f8SRahul Rameshbabu 	}
8533ab196f8SRahul Rameshbabu 
8543ab196f8SRahul Rameshbabu 	return 0;
8553ab196f8SRahul Rameshbabu 
8563ab196f8SRahul Rameshbabu err:
8573ab196f8SRahul Rameshbabu 	power_supply_unregister(shield_dev->battery_dev.psy);
8583ab196f8SRahul Rameshbabu 	return ret;
8593ab196f8SRahul Rameshbabu }
8603ab196f8SRahul Rameshbabu 
thunderstrike_create(struct hid_device * hdev)86109308562SRahul Rameshbabu static struct shield_device *thunderstrike_create(struct hid_device *hdev)
86209308562SRahul Rameshbabu {
86309308562SRahul Rameshbabu 	struct shield_device *shield_dev;
86409308562SRahul Rameshbabu 	struct thunderstrike *ts;
865f88af60eSRahul Rameshbabu 	int ret;
86609308562SRahul Rameshbabu 
86709308562SRahul Rameshbabu 	ts = devm_kzalloc(&hdev->dev, sizeof(*ts), GFP_KERNEL);
86809308562SRahul Rameshbabu 	if (!ts)
86909308562SRahul Rameshbabu 		return ERR_PTR(-ENOMEM);
87009308562SRahul Rameshbabu 
87109308562SRahul Rameshbabu 	ts->req_report_dmabuf = devm_kzalloc(
87209308562SRahul Rameshbabu 		&hdev->dev, THUNDERSTRIKE_HOSTCMD_REPORT_SIZE, GFP_KERNEL);
87309308562SRahul Rameshbabu 	if (!ts->req_report_dmabuf)
87409308562SRahul Rameshbabu 		return ERR_PTR(-ENOMEM);
87509308562SRahul Rameshbabu 
87609308562SRahul Rameshbabu 	shield_dev = &ts->base;
87709308562SRahul Rameshbabu 	shield_dev->hdev = hdev;
87809308562SRahul Rameshbabu 	shield_dev->codename = "Thunderstrike";
87909308562SRahul Rameshbabu 
88009308562SRahul Rameshbabu 	spin_lock_init(&ts->haptics_update_lock);
8813ab196f8SRahul Rameshbabu 	spin_lock_init(&ts->psy_stats_lock);
88209308562SRahul Rameshbabu 	INIT_WORK(&ts->hostcmd_req_work, thunderstrike_hostcmd_req_work_handler);
88309308562SRahul Rameshbabu 
88409308562SRahul Rameshbabu 	hid_set_drvdata(hdev, shield_dev);
88509308562SRahul Rameshbabu 
8863ab196f8SRahul Rameshbabu 	ts->id = ida_alloc(&thunderstrike_ida, GFP_KERNEL);
8873ab196f8SRahul Rameshbabu 	if (ts->id < 0)
8883ab196f8SRahul Rameshbabu 		return ERR_PTR(ts->id);
8893ab196f8SRahul Rameshbabu 
890cb818a04SRahul Rameshbabu 	ts->haptics_dev = shield_haptics_create(shield_dev, thunderstrike_play_effect);
8913ab196f8SRahul Rameshbabu 	if (IS_ERR(ts->haptics_dev)) {
8923ab196f8SRahul Rameshbabu 		hid_err(hdev, "Failed to create Thunderstrike haptics instance\n");
8933ab196f8SRahul Rameshbabu 		ret = PTR_ERR(ts->haptics_dev);
8943ab196f8SRahul Rameshbabu 		goto err_id;
8953ab196f8SRahul Rameshbabu 	}
8963ab196f8SRahul Rameshbabu 
8973ab196f8SRahul Rameshbabu 	ret = thunderstrike_psy_create(shield_dev);
8983ab196f8SRahul Rameshbabu 	if (ret) {
8993ab196f8SRahul Rameshbabu 		hid_err(hdev, "Failed to create Thunderstrike power supply instance\n");
9003ab196f8SRahul Rameshbabu 		goto err_haptics;
9013ab196f8SRahul Rameshbabu 	}
902cb818a04SRahul Rameshbabu 
903f88af60eSRahul Rameshbabu 	ret = thunderstrike_led_create(ts);
904f88af60eSRahul Rameshbabu 	if (ret) {
905f88af60eSRahul Rameshbabu 		hid_err(hdev, "Failed to create Thunderstrike LED instance\n");
9063ab196f8SRahul Rameshbabu 		goto err_psy;
907f88af60eSRahul Rameshbabu 	}
908f88af60eSRahul Rameshbabu 
9093ab196f8SRahul Rameshbabu 	timer_setup(&ts->psy_stats_timer, thunderstrike_psy_stats_timer_handler, 0);
91009308562SRahul Rameshbabu 
91109308562SRahul Rameshbabu 	hid_info(hdev, "Registered Thunderstrike controller\n");
91209308562SRahul Rameshbabu 	return shield_dev;
913f88af60eSRahul Rameshbabu 
9143ab196f8SRahul Rameshbabu err_psy:
9153ab196f8SRahul Rameshbabu 	power_supply_unregister(shield_dev->battery_dev.psy);
9163ab196f8SRahul Rameshbabu err_haptics:
917cb818a04SRahul Rameshbabu 	if (ts->haptics_dev)
918cb818a04SRahul Rameshbabu 		input_unregister_device(ts->haptics_dev);
9193ab196f8SRahul Rameshbabu err_id:
9203ab196f8SRahul Rameshbabu 	ida_free(&thunderstrike_ida, ts->id);
9213ab196f8SRahul Rameshbabu 	return ERR_PTR(ret);
92209308562SRahul Rameshbabu }
92309308562SRahul Rameshbabu 
android_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)92413d02c69SRahul Rameshbabu static int android_input_mapping(struct hid_device *hdev, struct hid_input *hi,
92513d02c69SRahul Rameshbabu 				 struct hid_field *field,
92613d02c69SRahul Rameshbabu 				 struct hid_usage *usage, unsigned long **bit,
92713d02c69SRahul Rameshbabu 				 int *max)
92813d02c69SRahul Rameshbabu {
92913d02c69SRahul Rameshbabu 	if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
93013d02c69SRahul Rameshbabu 		return 0;
93113d02c69SRahul Rameshbabu 
93213d02c69SRahul Rameshbabu 	switch (usage->hid & HID_USAGE) {
93313d02c69SRahul Rameshbabu 	case HID_USAGE_ANDROID_PLAYPAUSE_BTN:
93413d02c69SRahul Rameshbabu 		android_map_key(KEY_PLAYPAUSE);
93513d02c69SRahul Rameshbabu 		break;
93613d02c69SRahul Rameshbabu 	case HID_USAGE_ANDROID_VOLUMEUP_BTN:
93713d02c69SRahul Rameshbabu 		android_map_key(KEY_VOLUMEUP);
93813d02c69SRahul Rameshbabu 		break;
93913d02c69SRahul Rameshbabu 	case HID_USAGE_ANDROID_VOLUMEDOWN_BTN:
94013d02c69SRahul Rameshbabu 		android_map_key(KEY_VOLUMEDOWN);
94113d02c69SRahul Rameshbabu 		break;
94213d02c69SRahul Rameshbabu 	case HID_USAGE_ANDROID_SEARCH_BTN:
94313d02c69SRahul Rameshbabu 		android_map_key(BTN_Z);
94413d02c69SRahul Rameshbabu 		break;
94513d02c69SRahul Rameshbabu 	case HID_USAGE_ANDROID_HOME_BTN:
94613d02c69SRahul Rameshbabu 		android_map_key(BTN_MODE);
94713d02c69SRahul Rameshbabu 		break;
94813d02c69SRahul Rameshbabu 	case HID_USAGE_ANDROID_BACK_BTN:
94913d02c69SRahul Rameshbabu 		android_map_key(BTN_SELECT);
95013d02c69SRahul Rameshbabu 		break;
95113d02c69SRahul Rameshbabu 	default:
95213d02c69SRahul Rameshbabu 		return 0;
95313d02c69SRahul Rameshbabu 	}
95413d02c69SRahul Rameshbabu 
95513d02c69SRahul Rameshbabu 	return 1;
95613d02c69SRahul Rameshbabu }
95713d02c69SRahul Rameshbabu 
firmware_version_show(struct device * dev,struct device_attribute * attr,char * buf)95809308562SRahul Rameshbabu static ssize_t firmware_version_show(struct device *dev,
95909308562SRahul Rameshbabu 				     struct device_attribute *attr, char *buf)
96009308562SRahul Rameshbabu {
96109308562SRahul Rameshbabu 	struct hid_device *hdev = to_hid_device(dev);
96209308562SRahul Rameshbabu 	struct shield_device *shield_dev;
96309308562SRahul Rameshbabu 	int ret;
96409308562SRahul Rameshbabu 
96509308562SRahul Rameshbabu 	shield_dev = hid_get_drvdata(hdev);
96609308562SRahul Rameshbabu 
96709308562SRahul Rameshbabu 	if (test_bit(SHIELD_FW_VERSION_INITIALIZED, &shield_dev->initialized_flags))
96809308562SRahul Rameshbabu 		ret = sysfs_emit(buf, "0x%04X\n", shield_dev->fw_version);
96909308562SRahul Rameshbabu 	else
97009308562SRahul Rameshbabu 		ret = sysfs_emit(buf, NOT_INIT_STR "\n");
97109308562SRahul Rameshbabu 
97209308562SRahul Rameshbabu 	return ret;
97309308562SRahul Rameshbabu }
97409308562SRahul Rameshbabu 
97509308562SRahul Rameshbabu static DEVICE_ATTR_RO(firmware_version);
97609308562SRahul Rameshbabu 
hardware_version_show(struct device * dev,struct device_attribute * attr,char * buf)97709308562SRahul Rameshbabu static ssize_t hardware_version_show(struct device *dev,
97809308562SRahul Rameshbabu 				     struct device_attribute *attr, char *buf)
97909308562SRahul Rameshbabu {
98009308562SRahul Rameshbabu 	struct hid_device *hdev = to_hid_device(dev);
98109308562SRahul Rameshbabu 	struct shield_device *shield_dev;
98209308562SRahul Rameshbabu 	char board_revision_str[4];
98309308562SRahul Rameshbabu 	int ret;
98409308562SRahul Rameshbabu 
98509308562SRahul Rameshbabu 	shield_dev = hid_get_drvdata(hdev);
98609308562SRahul Rameshbabu 
98709308562SRahul Rameshbabu 	if (test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags)) {
98809308562SRahul Rameshbabu 		shield_strrev(board_revision_str, 4, shield_dev->board_info.revision);
98909308562SRahul Rameshbabu 		ret = sysfs_emit(buf, "%s BOARD_REVISION_%s (0x%04X)\n",
99009308562SRahul Rameshbabu 				 shield_dev->codename, board_revision_str,
99109308562SRahul Rameshbabu 				 shield_dev->board_info.revision);
99209308562SRahul Rameshbabu 	} else
99309308562SRahul Rameshbabu 		ret = sysfs_emit(buf, NOT_INIT_STR "\n");
99409308562SRahul Rameshbabu 
99509308562SRahul Rameshbabu 	return ret;
99609308562SRahul Rameshbabu }
99709308562SRahul Rameshbabu 
99809308562SRahul Rameshbabu static DEVICE_ATTR_RO(hardware_version);
99909308562SRahul Rameshbabu 
serial_number_show(struct device * dev,struct device_attribute * attr,char * buf)100009308562SRahul Rameshbabu static ssize_t serial_number_show(struct device *dev,
100109308562SRahul Rameshbabu 				  struct device_attribute *attr, char *buf)
100209308562SRahul Rameshbabu {
100309308562SRahul Rameshbabu 	struct hid_device *hdev = to_hid_device(dev);
100409308562SRahul Rameshbabu 	struct shield_device *shield_dev;
100509308562SRahul Rameshbabu 	int ret;
100609308562SRahul Rameshbabu 
100709308562SRahul Rameshbabu 	shield_dev = hid_get_drvdata(hdev);
100809308562SRahul Rameshbabu 
100909308562SRahul Rameshbabu 	if (test_bit(SHIELD_BOARD_INFO_INITIALIZED, &shield_dev->initialized_flags))
101009308562SRahul Rameshbabu 		ret = sysfs_emit(buf, "%s\n", shield_dev->board_info.serial_number);
101109308562SRahul Rameshbabu 	else
101209308562SRahul Rameshbabu 		ret = sysfs_emit(buf, NOT_INIT_STR "\n");
101309308562SRahul Rameshbabu 
101409308562SRahul Rameshbabu 	return ret;
101509308562SRahul Rameshbabu }
101609308562SRahul Rameshbabu 
101709308562SRahul Rameshbabu static DEVICE_ATTR_RO(serial_number);
101809308562SRahul Rameshbabu 
101909308562SRahul Rameshbabu static struct attribute *shield_device_attrs[] = {
102009308562SRahul Rameshbabu 	&dev_attr_firmware_version.attr,
102109308562SRahul Rameshbabu 	&dev_attr_hardware_version.attr,
102209308562SRahul Rameshbabu 	&dev_attr_serial_number.attr,
102309308562SRahul Rameshbabu 	NULL,
102409308562SRahul Rameshbabu };
102509308562SRahul Rameshbabu ATTRIBUTE_GROUPS(shield_device);
102609308562SRahul Rameshbabu 
shield_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)102709308562SRahul Rameshbabu static int shield_raw_event(struct hid_device *hdev, struct hid_report *report,
102809308562SRahul Rameshbabu 			    u8 *data, int size)
102909308562SRahul Rameshbabu {
103009308562SRahul Rameshbabu 	struct shield_device *dev = hid_get_drvdata(hdev);
103109308562SRahul Rameshbabu 
103209308562SRahul Rameshbabu 	return thunderstrike_parse_report(dev, report, data, size);
103309308562SRahul Rameshbabu }
103409308562SRahul Rameshbabu 
shield_probe(struct hid_device * hdev,const struct hid_device_id * id)103509308562SRahul Rameshbabu static int shield_probe(struct hid_device *hdev, const struct hid_device_id *id)
103609308562SRahul Rameshbabu {
103709308562SRahul Rameshbabu 	struct shield_device *shield_dev = NULL;
103809308562SRahul Rameshbabu 	struct thunderstrike *ts;
103909308562SRahul Rameshbabu 	int ret;
104009308562SRahul Rameshbabu 
104109308562SRahul Rameshbabu 	ret = hid_parse(hdev);
104209308562SRahul Rameshbabu 	if (ret) {
104309308562SRahul Rameshbabu 		hid_err(hdev, "Parse failed\n");
104409308562SRahul Rameshbabu 		return ret;
104509308562SRahul Rameshbabu 	}
104609308562SRahul Rameshbabu 
104709308562SRahul Rameshbabu 	switch (id->product) {
104809308562SRahul Rameshbabu 	case USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER:
104909308562SRahul Rameshbabu 		shield_dev = thunderstrike_create(hdev);
105009308562SRahul Rameshbabu 		break;
105109308562SRahul Rameshbabu 	}
105209308562SRahul Rameshbabu 
105309308562SRahul Rameshbabu 	if (unlikely(!shield_dev)) {
105409308562SRahul Rameshbabu 		hid_err(hdev, "Failed to identify SHIELD device\n");
105509308562SRahul Rameshbabu 		return -ENODEV;
105609308562SRahul Rameshbabu 	}
105709308562SRahul Rameshbabu 	if (IS_ERR(shield_dev)) {
105809308562SRahul Rameshbabu 		hid_err(hdev, "Failed to create SHIELD device\n");
105909308562SRahul Rameshbabu 		return PTR_ERR(shield_dev);
106009308562SRahul Rameshbabu 	}
106109308562SRahul Rameshbabu 
106209308562SRahul Rameshbabu 	ts = container_of(shield_dev, struct thunderstrike, base);
106309308562SRahul Rameshbabu 
106409308562SRahul Rameshbabu 	ret = hid_hw_start(hdev, HID_CONNECT_HIDINPUT);
106509308562SRahul Rameshbabu 	if (ret) {
106609308562SRahul Rameshbabu 		hid_err(hdev, "Failed to start HID device\n");
1067aa80f391SChristophe JAILLET 		goto err_ts_create;
106809308562SRahul Rameshbabu 	}
106909308562SRahul Rameshbabu 
107009308562SRahul Rameshbabu 	ret = hid_hw_open(hdev);
107109308562SRahul Rameshbabu 	if (ret) {
107209308562SRahul Rameshbabu 		hid_err(hdev, "Failed to open HID device\n");
107309308562SRahul Rameshbabu 		goto err_stop;
107409308562SRahul Rameshbabu 	}
107509308562SRahul Rameshbabu 
10763ab196f8SRahul Rameshbabu 	thunderstrike_device_init_info(shield_dev);
107709308562SRahul Rameshbabu 
107809308562SRahul Rameshbabu 	return ret;
107909308562SRahul Rameshbabu 
108009308562SRahul Rameshbabu err_stop:
108109308562SRahul Rameshbabu 	hid_hw_stop(hdev);
1082aa80f391SChristophe JAILLET err_ts_create:
1083aa80f391SChristophe JAILLET 	power_supply_unregister(ts->base.battery_dev.psy);
108409308562SRahul Rameshbabu 	if (ts->haptics_dev)
108509308562SRahul Rameshbabu 		input_unregister_device(ts->haptics_dev);
1086b07b6b27SChristophe JAILLET 	led_classdev_unregister(&ts->led_dev);
1087aa80f391SChristophe JAILLET 	ida_free(&thunderstrike_ida, ts->id);
108809308562SRahul Rameshbabu 	return ret;
108909308562SRahul Rameshbabu }
109009308562SRahul Rameshbabu 
shield_remove(struct hid_device * hdev)109109308562SRahul Rameshbabu static void shield_remove(struct hid_device *hdev)
109209308562SRahul Rameshbabu {
109309308562SRahul Rameshbabu 	struct shield_device *dev = hid_get_drvdata(hdev);
109409308562SRahul Rameshbabu 	struct thunderstrike *ts;
109509308562SRahul Rameshbabu 
109609308562SRahul Rameshbabu 	ts = container_of(dev, struct thunderstrike, base);
109709308562SRahul Rameshbabu 
109809308562SRahul Rameshbabu 	hid_hw_close(hdev);
10993ab196f8SRahul Rameshbabu 	power_supply_unregister(dev->battery_dev.psy);
110009308562SRahul Rameshbabu 	if (ts->haptics_dev)
110109308562SRahul Rameshbabu 		input_unregister_device(ts->haptics_dev);
11023ab196f8SRahul Rameshbabu 	led_classdev_unregister(&ts->led_dev);
11033ab196f8SRahul Rameshbabu 	ida_free(&thunderstrike_ida, ts->id);
11043ab196f8SRahul Rameshbabu 	del_timer_sync(&ts->psy_stats_timer);
110509308562SRahul Rameshbabu 	cancel_work_sync(&ts->hostcmd_req_work);
110609308562SRahul Rameshbabu 	hid_hw_stop(hdev);
110709308562SRahul Rameshbabu }
110809308562SRahul Rameshbabu 
110909308562SRahul Rameshbabu static const struct hid_device_id shield_devices[] = {
111009308562SRahul Rameshbabu 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_NVIDIA,
111109308562SRahul Rameshbabu 			       USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER) },
111209308562SRahul Rameshbabu 	{ HID_USB_DEVICE(USB_VENDOR_ID_NVIDIA,
111309308562SRahul Rameshbabu 			 USB_DEVICE_ID_NVIDIA_THUNDERSTRIKE_CONTROLLER) },
111409308562SRahul Rameshbabu 	{ }
111509308562SRahul Rameshbabu };
111609308562SRahul Rameshbabu MODULE_DEVICE_TABLE(hid, shield_devices);
111709308562SRahul Rameshbabu 
111809308562SRahul Rameshbabu static struct hid_driver shield_driver = {
111909308562SRahul Rameshbabu 	.name          = "shield",
112009308562SRahul Rameshbabu 	.id_table      = shield_devices,
112113d02c69SRahul Rameshbabu 	.input_mapping = android_input_mapping,
112209308562SRahul Rameshbabu 	.probe         = shield_probe,
112309308562SRahul Rameshbabu 	.remove        = shield_remove,
112409308562SRahul Rameshbabu 	.raw_event     = shield_raw_event,
112509308562SRahul Rameshbabu 	.driver = {
112609308562SRahul Rameshbabu 		.dev_groups = shield_device_groups,
112709308562SRahul Rameshbabu 	},
112809308562SRahul Rameshbabu };
112909308562SRahul Rameshbabu module_hid_driver(shield_driver);
113009308562SRahul Rameshbabu 
113109308562SRahul Rameshbabu MODULE_AUTHOR("Rahul Rameshbabu <rrameshbabu@nvidia.com>");
113209308562SRahul Rameshbabu MODULE_DESCRIPTION("HID Driver for NVIDIA SHIELD peripherals.");
113309308562SRahul Rameshbabu MODULE_LICENSE("GPL");
1134