/* * Copyright (C) 2008, Netronome Systems, Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms and conditions of the GNU General Public License, * version 2, as published by the Free Software Foundation. * * This program is distributed in the hope it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 Temple * Place - Suite 330, Boston, MA 02111-1307 USA. */ #include #include #include #include #include #include #include #include #include #include #include #ifdef CONFIG_X86 #include #endif LIST_HEAD(alldevs_list); spinlock_t pcidevs_lock = SPIN_LOCK_UNLOCKED; #define MAX_BUSES 256 static struct { u8 map; u8 bus; u8 devfn; } bus2bridge[MAX_BUSES]; /* bus2bridge_lock protects bus2bridge array */ static DEFINE_SPINLOCK(bus2bridge_lock); static struct pci_dev *alloc_pdev(u8 bus, u8 devfn) { struct pci_dev *pdev; list_for_each_entry ( pdev, &alldevs_list, alldevs_list ) if ( pdev->bus == bus && pdev->devfn == devfn ) return pdev; pdev = xmalloc(struct pci_dev); if ( !pdev ) return NULL; memset(pdev, 0, sizeof(struct pci_dev)); *((u8*) &pdev->bus) = bus; *((u8*) &pdev->devfn) = devfn; pdev->domain = NULL; INIT_LIST_HEAD(&pdev->msi_list); list_add(&pdev->alldevs_list, &alldevs_list); spin_lock_init(&pdev->msix_table_lock); return pdev; } static void free_pdev(struct pci_dev *pdev) { list_del(&pdev->alldevs_list); xfree(pdev); } struct pci_dev *pci_get_pdev(int bus, int devfn) { struct pci_dev *pdev = NULL; ASSERT(spin_is_locked(&pcidevs_lock)); list_for_each_entry ( pdev, &alldevs_list, alldevs_list ) if ( (pdev->bus == bus || bus == -1) && (pdev->devfn == devfn || devfn == -1) ) { return pdev; } return NULL; } struct pci_dev *pci_get_pdev_by_domain(struct domain *d, int bus, int devfn) { struct pci_dev *pdev = NULL; list_for_each_entry ( pdev, &alldevs_list, alldevs_list ) if ( (pdev->bus == bus || bus == -1) && (pdev->devfn == devfn || devfn == -1) && (pdev->domain == d) ) { return pdev; } return NULL; } /** * pci_enable_acs - enable ACS if hardware support it * @dev: the PCI device */ void pci_enable_acs(struct pci_dev *pdev) { int pos; u16 cap; u16 ctrl; u8 bus = pdev->bus; u8 dev = PCI_SLOT(pdev->devfn); u8 func = PCI_FUNC(pdev->devfn); if ( !iommu_enabled ) return; pos = pci_find_ext_capability(0, bus, pdev->devfn, PCI_EXT_CAP_ID_ACS); if (!pos) return; cap = pci_conf_read16(bus, dev, func, pos + PCI_ACS_CAP); ctrl = pci_conf_read16(bus, dev, func, pos + PCI_ACS_CTRL); /* Source Validation */ ctrl |= (cap & PCI_ACS_SV); /* P2P Request Redirect */ ctrl |= (cap & PCI_ACS_RR); /* P2P Completion Redirect */ ctrl |= (cap & PCI_ACS_CR); /* Upstream Forwarding */ ctrl |= (cap & PCI_ACS_UF); pci_conf_write16(bus, dev, func, pos + PCI_ACS_CTRL, ctrl); } int pci_add_device(u8 bus, u8 devfn, const struct pci_dev_info *info) { struct pci_dev *pdev; unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn); const char *pdev_type; int ret = -ENOMEM; if (!info) pdev_type = "device"; else if (info->is_extfn) pdev_type = "extended function"; else if (info->is_virtfn) { spin_lock(&pcidevs_lock); pdev = pci_get_pdev(info->physfn.bus, info->physfn.devfn); spin_unlock(&pcidevs_lock); if ( !pdev ) pci_add_device(info->physfn.bus, info->physfn.devfn, NULL); pdev_type = "virtual function"; } else return -EINVAL; spin_lock(&pcidevs_lock); pdev = alloc_pdev(bus, devfn); if ( !pdev ) goto out; if ( info ) pdev->info = *info; else if ( !pdev->vf_rlen[0] ) { unsigned int pos = pci_find_ext_capability(0, bus, devfn, PCI_EXT_CAP_ID_SRIOV); u16 ctrl = pci_conf_read16(bus, slot, func, pos + PCI_SRIOV_CTRL); if ( !pos ) /* Nothing */; else if ( !(ctrl & (PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE)) ) { unsigned int i; BUILD_BUG_ON(ARRAY_SIZE(pdev->vf_rlen) != PCI_SRIOV_NUM_BARS); for ( i = 0; i < PCI_SRIOV_NUM_BARS; ++i ) { unsigned int idx = pos + PCI_SRIOV_BAR + i * 4; u32 bar = pci_conf_read32(bus, slot, func, idx); u32 hi = 0; if ( (bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_IO ) { printk(XENLOG_WARNING "SR-IOV device %02x:%02x.%x with vf" " BAR%u in IO space\n", bus, slot, func, i); continue; } pci_conf_write32(bus, slot, func, idx, ~0); if ( (bar & PCI_BASE_ADDRESS_MEM_TYPE_MASK) == PCI_BASE_ADDRESS_MEM_TYPE_64 ) { if ( i >= PCI_SRIOV_NUM_BARS ) { printk(XENLOG_WARNING "SR-IOV device %02x:%02x.%x with" " 64-bit vf BAR in last slot\n", bus, slot, func); break;
/*
The MIT License (MIT)

Copyright (c) 2016 Fred Sundvik

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include "lcd_backlight.h"
#include <math.h>

static uint8_t current_hue = 0;
static uint8_t current_saturation = 0;
static uint8_t current_intensity = 0;
static uint8_t current_brightness = 0;

void lcd_backlight_init(void) {
    lcd_backlight_hal_init();
    lcd_backlight_color(current_hue, current_saturation, current_intensity);
}

// This code is based on Brian Neltner's blogpost and example code
// "Why every LED light should be using HSI colorspace".
// http://blog.saikoled.com/post/43693602826/why-every-led-light-should-be-using-hsi
static void hsi_to_rgb(float h, float s, float i, uint16_t* r_out, uint16_t* g_out, uint16_t* b_out) {
    unsigned int r, g, b;
    h = fmodf(h, 360.0f); // cycle h around to 0-360 degrees
    h = 3.14159f * h / 180.0f; // Convert to radians.
    s = s > 0.0f ? (s < 1.0f ? s : 1.0f) : 0.0f; // clamp s and i to interval [0,1]
    i = i > 0.0f ? (i < 1.0f ? i : 1.0f) : 0.0f;

    // Math! Thanks in part to Kyle Miller.
    if(h < 2.09439f) {
        r = 65535.0f * i/3.0f *(1.0f + s * cos(h) / cosf(1.047196667f - h));
        g = 65535.0f * i/3.0f *(1.0f + s *(1.0f - cosf(h) / cos(1.047196667f - h)));
        b = 65535.0f * i/3.0f *(1.0f - s);
    } else if(h < 4.188787) {
        h = h - 2.09439;
        g = 65535.0f * i/3.0f *(1.0f + s * cosf(h) / cosf(1.047196667f - h));
        b = 65535.0f * i/3.0f *(1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h)));
        r = 65535.0f * i/3.0f *(1.0f - s);
    } else {
        h = h - 4.188787;
        b = 65535.0f*i/3.0f * (1.0f + s * cosf(h) / cosf(1.047196667f - h));
        r = 65535.0f*i/3.0f * (1.0f + s * (1.0f - cosf(h) / cosf(1.047196667f - h)));
        g = 65535.0f*i/3.0f * (1.0f - s);
    }
    *r_out = r > 65535 ? 65535 : r;
    *g_out = g > 65535 ? 65535 : g;
    *b_out = b > 65535 ? 65535 : b;
}

void lcd_backlight_color(uint8_t hue, uint8_t saturation, uint8_t intensity) {
    uint16_t r, g, b;
    float hue_f = 360.0f * (float)hue / 255.0f;
    float saturation_f = (float)saturation / 255.0f;
    float intensity_f = (float)intensity / 255.0f;
    intensity_f *= (float)current_brightness / 255.0f;
    hsi_to_rgb(hue_f, saturation_f, intensity_f, &r, &g, &b);
	current_hue = hue;
	current_saturation = saturation;
	current_intensity = intensity;
	lcd_backlight_hal_color(r, g, b);
}

void lcd_backlight_brightness(uint8_t b) {
    current_brightness = b;
    lcd_backlight_color(current_hue, current_saturation, current_intensity);
}

uint8_t lcd_get_backlight_brightness(void) {
	return current_brightness;
}