1/*	$NetBSD: acpi_fan.c,v 1.6 2011/06/21 09:49:05 jruoho Exp $ */
2
3/*-
4 * Copyright (c) 2011 Jukka Ruohonen <jruohonen@iki.fi>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: acpi_fan.c,v 1.6 2011/06/21 09:49:05 jruoho Exp $");
31
32#include <sys/param.h>
33#include <sys/module.h>
34
35#include <dev/acpi/acpireg.h>
36#include <dev/acpi/acpivar.h>
37#include <dev/acpi/acpi_power.h>
38
39#include <dev/sysmon/sysmonvar.h>
40
41#define _COMPONENT		ACPI_RESOURCE_COMPONENT
42ACPI_MODULE_NAME		("acpi_fan")
43
44struct acpifan_softc {
45	device_t		 sc_dev;
46	struct acpi_devnode	*sc_node;
47	struct sysmon_envsys	*sc_sme;
48	envsys_data_t		 sc_sensor;
49};
50
51const char * const acpi_fan_ids[] = {
52	"PNP0C0B",
53	NULL
54};
55
56static int	acpifan_match(device_t, cfdata_t, void *);
57static void	acpifan_attach(device_t, device_t, void *);
58static int	acpifan_detach(device_t, int);
59static bool	acpifan_suspend(device_t, const pmf_qual_t *);
60static bool	acpifan_resume(device_t, const pmf_qual_t *);
61static bool	acpifan_shutdown(device_t, int);
62static bool	acpifan_sensor_init(device_t);
63static void	acpifan_sensor_state(void *);
64static void	acpifan_sensor_refresh(struct sysmon_envsys *,envsys_data_t *);
65
66CFATTACH_DECL_NEW(acpifan, sizeof(struct acpifan_softc),
67    acpifan_match, acpifan_attach, acpifan_detach, NULL);
68
69static int
70acpifan_match(device_t parent, cfdata_t match, void *aux)
71{
72	struct acpi_attach_args *aa = aux;
73
74	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
75		return 0;
76
77	return acpi_match_hid(aa->aa_node->ad_devinfo, acpi_fan_ids);
78}
79
80static void
81acpifan_attach(device_t parent, device_t self, void *aux)
82{
83	struct acpifan_softc *sc = device_private(self);
84	struct acpi_attach_args *aa = aux;
85	ACPI_HANDLE tmp;
86	ACPI_STATUS rv;
87
88	sc->sc_sme = NULL;
89	sc->sc_dev = self;
90	sc->sc_node = aa->aa_node;
91
92	aprint_naive("\n");
93	aprint_normal(": ACPI Fan\n");
94
95	if (acpifan_sensor_init(self) != true)
96		aprint_error_dev(self, "failed to initialize\n");
97
98	(void)acpi_power_register(sc->sc_node->ad_handle);
99	(void)pmf_device_register1(self, acpifan_suspend,
100	    acpifan_resume, acpifan_shutdown);
101
102	rv = AcpiGetHandle(sc->sc_node->ad_handle, "_FIF", &tmp);
103
104	if (ACPI_SUCCESS(rv))
105		aprint_verbose_dev(self, "ACPI 4.0 functionality present\n");
106}
107
108static int
109acpifan_detach(device_t self, int flags)
110{
111	struct acpifan_softc *sc = device_private(self);
112
113	(void)acpi_power_set(sc->sc_node->ad_handle, ACPI_STATE_D0);
114
115	pmf_device_deregister(self);
116	acpi_power_deregister(sc->sc_node->ad_handle);
117
118	if (sc->sc_sme != NULL)
119		sysmon_envsys_unregister(sc->sc_sme);
120
121	return 0;
122}
123
124static bool
125acpifan_suspend(device_t self, const pmf_qual_t *qual)
126{
127	struct acpifan_softc *sc = device_private(self);
128
129	(void)acpi_power_set(sc->sc_node->ad_handle, ACPI_STATE_D0);
130
131	return true;
132}
133
134static bool
135acpifan_resume(device_t self, const pmf_qual_t *qual)
136{
137	struct acpifan_softc *sc = device_private(self);
138
139	(void)acpi_power_set(sc->sc_node->ad_handle, ACPI_STATE_D3);
140
141	return true;
142}
143
144static bool
145acpifan_shutdown(device_t self, int how)
146{
147	struct acpifan_softc *sc = device_private(self);
148
149	(void)acpi_power_set(sc->sc_node->ad_handle, ACPI_STATE_D0);
150
151	return true;
152}
153
154static bool
155acpifan_sensor_init(device_t self)
156{
157	struct acpifan_softc *sc = device_private(self);
158	int state;
159
160	if (acpi_power_get(sc->sc_node->ad_handle, &state) != true)
161		return false;
162
163	sc->sc_sme = sysmon_envsys_create();
164
165	acpifan_sensor_state(self);
166	sc->sc_sensor.units = ENVSYS_INDICATOR;
167
168	(void)strlcpy(sc->sc_sensor.desc, "state", sizeof(sc->sc_sensor.desc));
169
170	sc->sc_sme->sme_cookie = self;
171	sc->sc_sme->sme_flags = SME_POLL_ONLY;
172	sc->sc_sme->sme_name = device_xname(self);
173	sc->sc_sme->sme_refresh = acpifan_sensor_refresh;
174
175	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
176		goto fail;
177
178	if (sysmon_envsys_register(sc->sc_sme) != 0)
179		goto fail;
180
181	return true;
182
183fail:
184	sysmon_envsys_destroy(sc->sc_sme);
185	sc->sc_sme = NULL;
186
187	return false;
188}
189
190static void
191acpifan_sensor_state(void *arg)
192{
193	struct acpifan_softc *sc;
194	device_t self;
195	int state;
196
197	self = arg;
198	sc = device_private(self);
199	state = ACPI_STATE_ERROR;
200
201	(void)acpi_power_get(sc->sc_node->ad_handle, &state);
202
203	switch (state) {
204
205	case ACPI_STATE_D0:
206	case ACPI_STATE_D1:
207	case ACPI_STATE_D2:
208		sc->sc_sensor.value_cur = 1;
209		sc->sc_sensor.state = ENVSYS_SVALID;
210		break;
211
212	case ACPI_STATE_D3:
213		sc->sc_sensor.value_cur = 0;
214		sc->sc_sensor.state = ENVSYS_SVALID;
215		break;
216
217	default:
218		sc->sc_sensor.state = ENVSYS_SINVALID;
219		break;
220	}
221}
222
223static void
224acpifan_sensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
225{
226	device_t self = sme->sme_cookie;
227
228	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpifan_sensor_state, self);
229}
230
231MODULE(MODULE_CLASS_DRIVER, acpifan, NULL);
232
233#ifdef _MODULE
234#include "ioconf.c"
235#endif
236
237static int
238acpifan_modcmd(modcmd_t cmd, void *aux)
239{
240	int rv = 0;
241
242	switch (cmd) {
243
244	case MODULE_CMD_INIT:
245
246#ifdef _MODULE
247		rv = config_init_component(cfdriver_ioconf_acpifan,
248		    cfattach_ioconf_acpifan, cfdata_ioconf_acpifan);
249#endif
250		break;
251
252	case MODULE_CMD_FINI:
253
254#ifdef _MODULE
255		rv = config_fini_component(cfdriver_ioconf_acpifan,
256		    cfattach_ioconf_acpifan, cfdata_ioconf_acpifan);
257#endif
258		break;
259
260	default:
261		rv = ENOTTY;
262	}
263
264	return rv;
265}
266