blob: dbac1664166277ba2117b2dde68677699200cfd6 [file] [log] [blame]
Sandeep Singh4f567b92020-10-10 01:31:36 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * AMD MP2 PCIe communication driver
4 * Copyright 2020 Advanced Micro Devices, Inc.
5 *
6 * Authors: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
7 * Sandeep Singh <Sandeep.singh@amd.com>
8 */
9
10#include <linux/bitops.h>
11#include <linux/delay.h>
12#include <linux/dma-mapping.h>
13#include <linux/interrupt.h>
14#include <linux/io-64-nonatomic-lo-hi.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17
18#include "amd_sfh_pcie.h"
19
20#define DRIVER_NAME "pcie_mp2_amd"
21#define DRIVER_DESC "AMD(R) PCIe MP2 Communication Driver"
22
Sandeep Singh4b393f02020-10-28 14:30:10 +053023#define ACEL_EN BIT(0)
24#define GYRO_EN BIT(1)
25#define MAGNO_EN BIT(2)
Sandeep Singh4f567b92020-10-10 01:31:36 +053026#define ALS_EN BIT(19)
27
28void amd_start_sensor(struct amd_mp2_dev *privdata, struct amd_mp2_sensor_info info)
29{
30 union sfh_cmd_param cmd_param;
31 union sfh_cmd_base cmd_base;
32
33 /* fill up command register */
34 memset(&cmd_base, 0, sizeof(cmd_base));
35 cmd_base.s.cmd_id = ENABLE_SENSOR;
36 cmd_base.s.period = info.period;
37 cmd_base.s.sensor_id = info.sensor_idx;
38
39 /* fill up command param register */
40 memset(&cmd_param, 0, sizeof(cmd_param));
41 cmd_param.s.buf_layout = 1;
42 cmd_param.s.buf_length = 16;
43
Arnd Bergmannde304912021-01-03 14:53:55 +010044 writeq(info.dma_address, privdata->mmio + AMD_C2P_MSG2);
Sandeep Singh4f567b92020-10-10 01:31:36 +053045 writel(cmd_param.ul, privdata->mmio + AMD_C2P_MSG1);
46 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0);
47}
48
49void amd_stop_sensor(struct amd_mp2_dev *privdata, u16 sensor_idx)
50{
51 union sfh_cmd_base cmd_base;
52
53 /* fill up command register */
54 memset(&cmd_base, 0, sizeof(cmd_base));
55 cmd_base.s.cmd_id = DISABLE_SENSOR;
56 cmd_base.s.period = 0;
57 cmd_base.s.sensor_id = sensor_idx;
58
59 writeq(0x0, privdata->mmio + AMD_C2P_MSG2);
60 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0);
61}
62
63void amd_stop_all_sensors(struct amd_mp2_dev *privdata)
64{
65 union sfh_cmd_base cmd_base;
66
67 /* fill up command register */
68 memset(&cmd_base, 0, sizeof(cmd_base));
69 cmd_base.s.cmd_id = STOP_ALL_SENSORS;
70 cmd_base.s.period = 0;
71 cmd_base.s.sensor_id = 0;
72
73 writel(cmd_base.ul, privdata->mmio + AMD_C2P_MSG0);
74}
75
76int amd_mp2_get_sensor_num(struct amd_mp2_dev *privdata, u8 *sensor_id)
77{
78 int activestatus, num_of_sensors = 0;
79
80 privdata->activecontrolstatus = readl(privdata->mmio + AMD_P2C_MSG3);
81 activestatus = privdata->activecontrolstatus >> 4;
82 if (ACEL_EN & activestatus)
83 sensor_id[num_of_sensors++] = accel_idx;
84
85 if (GYRO_EN & activestatus)
86 sensor_id[num_of_sensors++] = gyro_idx;
87
88 if (MAGNO_EN & activestatus)
89 sensor_id[num_of_sensors++] = mag_idx;
90
91 if (ALS_EN & activestatus)
92 sensor_id[num_of_sensors++] = als_idx;
93
94 return num_of_sensors;
95}
96
97static void amd_mp2_pci_remove(void *privdata)
98{
99 amd_sfh_hid_client_deinit(privdata);
100 amd_stop_all_sensors(privdata);
101}
102
103static int amd_mp2_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
104{
105 struct amd_mp2_dev *privdata;
106 int rc;
107
108 privdata = devm_kzalloc(&pdev->dev, sizeof(*privdata), GFP_KERNEL);
109 if (!privdata)
110 return -ENOMEM;
111
112 privdata->pdev = pdev;
113 pci_set_drvdata(pdev, privdata);
114 rc = pcim_enable_device(pdev);
115 if (rc)
116 return rc;
117
118 rc = pcim_iomap_regions(pdev, BIT(2), DRIVER_NAME);
119 if (rc)
120 return rc;
121
122 privdata->mmio = pcim_iomap_table(pdev)[2];
123 pci_set_master(pdev);
124 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
125 if (rc) {
126 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
127 return rc;
128 }
129 rc = devm_add_action_or_reset(&pdev->dev, amd_mp2_pci_remove, privdata);
130 if (rc)
131 return rc;
132
133 return amd_sfh_hid_client_init(privdata);
134}
135
136static const struct pci_device_id amd_mp2_pci_tbl[] = {
137 { PCI_VDEVICE(AMD, PCI_DEVICE_ID_AMD_MP2) },
138 { }
139};
140MODULE_DEVICE_TABLE(pci, amd_mp2_pci_tbl);
141
142static struct pci_driver amd_mp2_pci_driver = {
143 .name = DRIVER_NAME,
144 .id_table = amd_mp2_pci_tbl,
145 .probe = amd_mp2_pci_probe,
146};
147module_pci_driver(amd_mp2_pci_driver);
148
149MODULE_DESCRIPTION(DRIVER_DESC);
150MODULE_LICENSE("Dual BSD/GPL");
151MODULE_AUTHOR("Shyam Sundar S K <Shyam-sundar.S-k@amd.com>");
152MODULE_AUTHOR("Sandeep Singh <Sandeep.singh@amd.com>");